jQuery UI Draggable - connectToSortable without helper clone - or how to move items from one list to another

前端 未结 2 1413
长发绾君心
长发绾君心 2021-01-01 09:16

Have a look at this example.

The docs says that:

helper must be set to \'clone\' in order to work flawlessly

<
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 09:51

    I've been struggling with this same problem. If I do indeed set the helper to 'original' I get all kinds of jumpy and weird behavior.

    Not sure if it will serve your purposes, but I wrote some code to listen to the drag start/stop events and also the sortable update event.

    On drag start the item is hidden from the list. If the item is not transferred to the other list (determined by a boolean) then the item is unhidden. However, if the item is transferred then the original is removed.

    $(function() {
    var transferred = false;
    $('#draggable li').draggable({
        connectToSortable: '#sortable',
        helper: 'clone',
        start: function(event, ui)
        {
            $(this).hide();
        },
        stop: function(event, ui)
        {
            if(!transferred)
                $(this).show();
            else
            {
                $(this).remove();
                transferred = false;
            }
        }
    });
    
    $('#sortable').sortable({
        receive: function(event, ui)
        {
            transferred = true;
        }
    });
    

    }); ​

    Modified jsfiddle example. (http://jsfiddle.net/xD2dW/12/)

    Obviously the code can be modified to meet your specific needs, but I hope this is at least a good start.

提交回复
热议问题