场景:拖动一个或多个元素并将它们放置在另一个列表中,但不要从源列表中删除拖动的元素。
为了允许多次拖动,我使用了Aaron Blenkush在这里发布的代码。
我已经进行了一些更改以适应我的需求,例如修复了Shift键问题。
$("ul").on('click', 'li', function (e) {
if (e.ctrlKey || e.metaKey) { //meta key for mac users
$(this).toggleClass("ui-selected");
$(this).parent().parent().siblings().children('ul').children().removeClass('ui-selected');
}
else if (e.shiftKey) {
// $(this).toggleClass("selected");
var list = $(this).parent();
var first = list.find('.ui-selected').first().index();
// var last = list.find('.selected').last().index();
var last = $(this).index();
if (first == -1 || last == -1) {
return false;
}
if (last < first) {
firstHolder = first;
first = last;
last = firstHolder;
}
for (i = first; i <= last ; i++) {
list.children().eq(i).addClass("ui-selected");
}
}
else {
$(this).addClass("ui-selected").siblings().removeClass('ui-selected'); //select only clicked element and unselecting other elements
//to Remove selected Class from 2nd UL li
$(this).parent().parent().siblings().children('ul').children().removeClass('ui-selected');
}
})
.sortable({
connectWith: "ul",
delay: 150, //Needed to prevent accidental drag when trying to select
revert: 0,
cursor: "move",
disabled: false,
placeholder: 'placeholder',
// handle: ".handle",
helper: function (e, item) {
//Basically, if you grab an unhighlighted item to drag, it will deselect (unhighlight) everything else
if (!item.hasClass('ui-selected')) {
item.addClass('ui-selected').siblings().removeClass('ui-selected');
}
//////////////////////////////////////////////////////////////////////
//HERE'S HOW TO PASS THE SELECTED ITEMS TO THE `stop()` FUNCTION:
//Clone the selected items into an array
var elements = item.parent().children('.ui-selected').clone();
//Add a property to `item` called 'multidrag` that contains the selected items, then remove the selected items from the source list
item.data('multidrag', elements)
//.siblings('.ui-selected').remove();
//Now the selected items exist in memory, attached to the `item`, so we can access them later when we get to the `stop()` callback
//Create the helper
var helper = $('<li/>');
return helper.append(elements);
},
start: function(event, ui) {
//$(ui.item).show();
var elements = ui.item.data('multidrag');
ui.item.after(elements);
//clone = $(ui.item).clone();
//before = $(ui.item).prev();
},
stop: function (e, ui) {
//Now we access those items that we stored in `item`s data!
var elements = ui.item.data('multidrag');
//`elements` now contains the originally selected items from the source list (the dragged items)!!
//Finally I insert the selected items after the `item`, then remove the `item`, since item is a duplicate of one of the selected items.
ui.item.after(elements).remove();
// $(ui.item).show();
//$(this).sortable('cancel');
}
});
问题: 它从源列表中删除了被拖动的元素。我对此问题进行了一些研究。有些人建议在helper方法中使用克隆,但当我传递给返回多个元素的函数时,我不能使用克隆。在Firebug中,当我拖动元素时,它会在它们上面放置style=display:none,但当我将元素放下时,它会从源列表中删除它们。 更新 我对答案进行了一些修改,使其更简单。 更新后的答案 存在一些问题,例如当从第一个列表中删除li并移到第二个列表时,如果将该li再次从第二个列表拖回第一个列表,则会删除元素。 解决方案是当项目被拖放到第二个列表中时,删除ui-selected类。
希望能对某些人有所帮助 :)
One
或多个任何数字,并且你将它们都移动到另一个列表中,那么原始列表中只会保留一个One
而不是两个...我不知道为什么你想在列表中有多个相同的东西,所以我会再想一个fiddle,在列表中有重复项时它会将它们删除。 - Trevor