我正在使用这种方法http://jqueryui.com/demos/sortable/#connect-lists将两个列表连接在一起。我希望能够从列表 A 拖动到列表 B,但是当项目被放下时,我需要保留原始项目仍然在列表 A 中。我查看了选项和事件,但我认为没有类似的功能。有什么解决方法吗?
我正在使用这种方法http://jqueryui.com/demos/sortable/#connect-lists将两个列表连接在一起。我希望能够从列表 A 拖动到列表 B,但是当项目被放下时,我需要保留原始项目仍然在列表 A 中。我查看了选项和事件,但我认为没有类似的功能。有什么解决方法吗?
$("#sortable1").sortable({
connectWith: ".connectedSortable",
forcePlaceholderSize: false,
helper: function (e, li) {
copyHelper = li.clone().insertAfter(li);
return li.clone();
},
stop: function () {
copyHelper && copyHelper.remove();
}
});
$(".connectedSortable").sortable({
receive: function (e, ui) {
copyHelper = null;
}
});
copyHelper = li.clone().insertAfter(li);
中删除insertAfter(li)
将不会在原始列表中创建副本。如果您想将该项复制到多个列表中,但不包括原始列表,则可以这样做。 - Huy Phạm作为入门,先看一下这个,并且也要阅读@Erez的答案。
$(function () {
$("#sortable1").sortable({
connectWith: ".connectedSortable",
remove: function (event, ui) {
ui.item.clone().appendTo('#sortable2');
$(this).sortable('cancel');
}
}).disableSelection();
$("#sortable2").sortable({
connectWith: ".connectedSortable"
}).disableSelection();
});
Erez的解决方案对我有效,但我发现它缺乏封装性是令人沮丧的。我建议使用以下解决方案来避免使用全局变量:
$("#sortable1").sortable({
connectWith: ".connectedSortable",
helper: function (e, li) {
this.copyHelper = li.clone().insertAfter(li);
$(this).data('copied', false);
return li.clone();
},
stop: function () {
var copied = $(this).data('copied');
if (!copied) {
this.copyHelper.remove();
}
this.copyHelper = null;
}
});
$("#sortable2").sortable({
receive: function (e, ui) {
ui.sender.data('copied', true);
}
});
这里有一个 jsFiddle: http://jsfiddle.net/v265q/190/
我知道这是老的内容,但是我无法让Erez的答案生效,而Thorsten的答案对我需要的项目来说不够。以下方法似乎完全符合我的需求:
$("#sortable2, #sortable1").sortable({
connectWith: ".connectedSortable",
remove: function (e, li) {
copyHelper = li.item.clone().insertAfter(li.item);
$(this).sortable('cancel');
return li.item.clone();
}
}).disableSelection();
return li.item.clone();
甚至都不需要。 - mplungjanreturn li.item.clone();
$("#sortable2, #sortable1").sortable({
connectWith: ".connectedSortable",
remove: function (e, li) {
li.item.clone().insertAfter(li.item);
$(this).sortable('cancel');
return li.item.clone();
}
}).disableSelection();
copyHelper.click(function () {
var icon = $(this);
icon.toggleClass("ui-icon-minusthick ui-icon-plusthick");
icon.closest(".portlet").find(".portlet-content").toggle();
});
我花了一段时间才弄明白,希望能帮到别人。