使用jquery sortable时如何复制项目?

时间:2021-02-07 07:38:17

I am using this method http://jqueryui.com/demos/sortable/#connect-lists to connect two lists that i have. I want to be able to drag from list A to list B but when the item is dropped, i need to keep the original one still in list A. I checked the options and events but I believe there is nothing like that. Any approaches?

我正在使用这种方法http://jqueryui.com/demos/sortable/#connect-lists连接我拥有的两个列表。我希望能够从列表A拖动到列表B但是当项目被删除时,我需要将原始列表保留在列表A中。我检查了选项和事件,但我相信没有类似的东西。任何方法?

6 个解决方案

#1


24  

For a beginning, have a look at this, and read @Erez answer, too.

首先,看看这个,并阅读@Erez的答案。

$(function () {
    $("#sortable1").sortable({
        connectWith: ".connectedSortable",
        remove: function (event, ui) {
            ui.item.clone().appendTo('#sortable2');
            $(this).sortable('cancel');
        }
    }).disableSelection();

    $("#sortable2").sortable({
        connectWith: ".connectedSortable"
    }).disableSelection();
});

#2


80  

$("#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;
    }
});

#3


27  

Erez' solution works for me, but I found its lack of encapsulation frustrating. I'd propose using the following solution to avoid global variable usage:

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);
    }
});

Here's a jsFiddle: http://jsfiddle.net/v265q/190/

这是一个jsFiddle:http://jsfiddle.net/v265q/190/

#4


7  

I know this is old, but I could not get Erez's answer to work, and Thorsten's didn't cut it for the project I need it for. This seems to work exactly how I need:

我知道这已经过时了,但我无法得到Erez的工作答案,而Thorsten没有为我需要的项目削减它。这似乎完全符合我的需要:

$("#sortable2, #sortable1").sortable({
    connectWith: ".connectedSortable",
    remove: function (e, li) {
        copyHelper = li.item.clone().insertAfter(li.item);
        $(this).sortable('cancel');
        return li.clone();
    }
}).disableSelection();

#5


5  

The answer of abuser2582707 works best for me. Except one error: You need to change the return to

abuser2582707的答案最适合我。除了一个错误:您需要将返回更改为

return li.item.clone();

So it should be:

所以它应该是:

$("#sortable2, #sortable1").sortable({
    connectWith: ".connectedSortable",
    remove: function (e, li) {
        li.item.clone().insertAfter(li.item);
        $(this).sortable('cancel');
        return li.item.clone();
    }
}).disableSelection();

#6


0  

When using Erez's solution but for connecting 2 sortable portlets (basis was the portlet example code from http://jqueryui.com/sortable/#portlets), the toggle on the clone would not work. I added the following line before 'return li.clone();' to make it work.

使用Erez的解决方案但是为了连接2个可排序的portlet(基础是来自http://jqueryui.com/sortable/#portlets的portlet示例代码),克隆上的切换不起作用。我在'return li.clone();'之前添加了以下行。使它工作。

copyHelper.click(function () {
    var icon = $(this);
    icon.toggleClass("ui-icon-minusthick ui-icon-plusthick");
    icon.closest(".portlet").find(".portlet-content").toggle();
});

This took me a while to figure out so I hope it helps someone.

我花了一段时间才弄明白,所以我希望它可以帮到某人。

#1


24  

For a beginning, have a look at this, and read @Erez answer, too.

首先,看看这个,并阅读@Erez的答案。

$(function () {
    $("#sortable1").sortable({
        connectWith: ".connectedSortable",
        remove: function (event, ui) {
            ui.item.clone().appendTo('#sortable2');
            $(this).sortable('cancel');
        }
    }).disableSelection();

    $("#sortable2").sortable({
        connectWith: ".connectedSortable"
    }).disableSelection();
});

#2


80  

$("#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;
    }
});

#3


27  

Erez' solution works for me, but I found its lack of encapsulation frustrating. I'd propose using the following solution to avoid global variable usage:

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);
    }
});

Here's a jsFiddle: http://jsfiddle.net/v265q/190/

这是一个jsFiddle:http://jsfiddle.net/v265q/190/

#4


7  

I know this is old, but I could not get Erez's answer to work, and Thorsten's didn't cut it for the project I need it for. This seems to work exactly how I need:

我知道这已经过时了,但我无法得到Erez的工作答案,而Thorsten没有为我需要的项目削减它。这似乎完全符合我的需要:

$("#sortable2, #sortable1").sortable({
    connectWith: ".connectedSortable",
    remove: function (e, li) {
        copyHelper = li.item.clone().insertAfter(li.item);
        $(this).sortable('cancel');
        return li.clone();
    }
}).disableSelection();

#5


5  

The answer of abuser2582707 works best for me. Except one error: You need to change the return to

abuser2582707的答案最适合我。除了一个错误:您需要将返回更改为

return li.item.clone();

So it should be:

所以它应该是:

$("#sortable2, #sortable1").sortable({
    connectWith: ".connectedSortable",
    remove: function (e, li) {
        li.item.clone().insertAfter(li.item);
        $(this).sortable('cancel');
        return li.item.clone();
    }
}).disableSelection();

#6


0  

When using Erez's solution but for connecting 2 sortable portlets (basis was the portlet example code from http://jqueryui.com/sortable/#portlets), the toggle on the clone would not work. I added the following line before 'return li.clone();' to make it work.

使用Erez的解决方案但是为了连接2个可排序的portlet(基础是来自http://jqueryui.com/sortable/#portlets的portlet示例代码),克隆上的切换不起作用。我在'return li.clone();'之前添加了以下行。使它工作。

copyHelper.click(function () {
    var icon = $(this);
    icon.toggleClass("ui-icon-minusthick ui-icon-plusthick");
    icon.closest(".portlet").find(".portlet-content").toggle();
});

This took me a while to figure out so I hope it helps someone.

我花了一段时间才弄明白,所以我希望它可以帮到某人。