最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI sortable: Move clone but keep original - Stack Overflow

programmeradmin2浏览0评论

Right now I have a column of elements and I have it to the point where where I drag, it clones but when I drop, it removes the original as well.

$( ".column" ).sortable({
        helper: 'clone',
        connectWith: ".column",
        connectWith: ".grid",
        start: function(e, ui){
            ui.placeholder.height(ui.item.height());
             $(".column" ).find('.portlet:hidden').show()
             console.log('started')
        },
        stop: function(event, ui) {
            $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body');
        }
    });

How can I keep the original where its at (without re-appending it to the column) and have the clone move to the desired location?

Right now I have a column of elements and I have it to the point where where I drag, it clones but when I drop, it removes the original as well.

$( ".column" ).sortable({
        helper: 'clone',
        connectWith: ".column",
        connectWith: ".grid",
        start: function(e, ui){
            ui.placeholder.height(ui.item.height());
             $(".column" ).find('.portlet:hidden').show()
             console.log('started')
        },
        stop: function(event, ui) {
            $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body');
        }
    });

How can I keep the original where its at (without re-appending it to the column) and have the clone move to the desired location?

Share Improve this question asked Jan 7, 2013 at 16:11 user240993user240993 2
  • 1 Beware of generating invalid HTML (duplicate IDs) when using clone(). – jbabey Commented Jan 7, 2013 at 16:30
  • 1 This question is very similar and has a great answer -- look at the top-voted one, not the accepted one. – Coderer Commented Dec 30, 2013 at 11:09
Add a comment  | 

2 Answers 2

Reset to default 13

Use $(this).sortable('cancel') inside the stop event handler to revert the item back to it's original list/position. http://api.jqueryui.com/sortable/#method-cancel

$( ".column" ).sortable({
        helper: 'clone',
        connectWith: ".column",
        connectWith: ".grid",
        start: function(e, ui){
            ui.placeholder.height(ui.item.height());
             $(".column" ).find('.portlet:hidden').show()
             console.log('started')
        },
        stop: function(event, ui) {
            $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body');
            $(this).sortable('cancel');
        }
    });

UPDATE:

To append the element to the second list in the location the item was dropping, do something like the following:

stop: function(event, ui) {
            var toListID = ui.item.parent().attr('id');   
            var idx = $('#' + toListID).children().index($(ui.item[0]));
            if(idx== -1)return;
            var elm = $(ui.item[0]).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone');
            $('#' + toListID).children(':eq('+idx+')').after(elm);
            $(this).sortable('cancel');
        }

See fiddle for full demo

If anyone using new version just use:

revert: true

More details here: jQuery Sortable - Keep it in original list

发布评论

评论列表(0)

  1. 暂无评论