jQuery swap item with connected list -
i'm working default jqueryui connected sortable list. need (as far know) not default functionality. i'm hoping here knows easy way accomplish want (maybe plugin?). coulddn't find direct solutions.
like said, i'm working default connect sortable: http://jsfiddle.net/klvdn/
this works. can drag , drop items 1 list another. want able drag item 1 list, drop ontop of item in other list , make them swap.
so in steps;
- i drag "item 1" left list
- i drop onto "item 3" right list
- "item 1" positioned on place of "item 3"
- "item3" moved left list on spot "item 1" was.
is possible somehow?
my solution
this did eventually.
i needed way drop item 1 list ontop of item in list. when dropped, item underneath should automatically placed in other list. thus, wapping 2 items.
first added 3 properties connected list:
var _index = null; $("#field-mylist, #test-mylist").sortable({ connectwith: ".mylist", placeholder: 'ui-placeholder', // <-- important change: function (event, ui) { markforreplacement('mylist', ui); }, update: function (event, ui) { updateconnectedlists('mylist'); } }).disableselection(); and 2 functions called change , update event:
function markforreplacement(position, ui) { var total = 0; $('#field-' + position + ' .player').each(function (index) { $(this).removeclass("selected-item"); total++; }); var index = ui.placeholder.index(); if (total < (index + 2)) { $('#field-' + position + ' div:eq(' + (index - 1) + ')').addclass("selected-item"); _index = (index - 1); } else { $('#field-' + position + ' div:nth-child(' + (index + 2) + ')').addclass("selected-item"); _index = (index + 2); } } function updateconnectedlists(position) { if (_index === null) return; $('#field-' + position + ' div:nth-child(' + (_index) + ')').insertafter($("#test-" + position + " .player")); _index = null; // reset class styles $('#test-' + position + ' .player').each(function (index) { $(this).removeclass("selected-item"); }); $('#test-' + position).sortable('refresh'); } one more important thing add following css style:
.ui-placeholder { float: left; } .selected-item{ border-color: #ff6600 !important; } with ui-placeholder can place item ontop of item (before releasing it). .selected-item class gives border element moved other list (so underlaying item).
you can check plugin swap though there online demo seem down.
disclaimer haven't tried plugin myself.
this post on so on same lines.
this can helpful index of element moved.
Comments
Post a Comment