【问题标题】:JQuery drag & drop sortable connect listJQuery拖放可排序连接列表
【发布时间】:2015-07-08 07:54:07
【问题描述】:

从JQueryUI demos 的示例开始,我正在尝试禁用左侧列表的排序,并且左侧的项目必须被复制而不是移动, 有没有可能?

I've created a jsfiddle for it

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

通过使用可拖动和可放置交互,我可以更接近,但我无法像使用 sortable (jsfiddle here) 那样将元素拖放到特定位置:

$(function() {

    $( "#sortable1 li" ).draggable({
        appendTo: "body",
        helper: "clone"
    });
    $( "#sortable2" ).droppable({
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        accept: ":not(.ui-sortable-helper)",
        drop: function( event, ui ) {
            $( this ).find( ".placeholder" ).remove();
            $( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
        }
    }).sortable({
        items: "li:not(.placeholder)",
        sort: function() {
            $( this ).removeClass( "ui-state-default" );
        }
    });
});

【问题讨论】:

    标签: jquery jquery-ui drag-and-drop jquery-ui-sortable


    【解决方案1】:

    我找到了解决方案。以下是修复代码:

    $(function() {
    
        $( "#sortable1 li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( "#sortable2" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function (event, ui) {
    
                if ($(this).find(".placeholder").length > 0) //add first element when cart is empty
                {
                    $(this).find(".placeholder").remove();
                    $("<li></li>").text(ui.draggable.text()).appendTo(this);
                } else {
                    var i = 0; //used as flag to find out if element added or not
                    $(this).children('li').each(function () {
    
                        if ($(this).offset().top >= ui.offset.top) //compare
                        {
                            $("<li></li>").text(ui.draggable.text()).insertBefore($(this));
                            i = 1;
                            return false; //break loop
                        }
                    })
    
                    if (i != 1) //if element dropped at the end of cart
                    {
                        $("<li></li>").text(ui.draggable.text()).appendTo(this);
                    }
                }
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 2016-09-14
      • 1970-01-01
      • 2015-11-20
      • 2012-05-31
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2010-10-18
      相关资源
      最近更新 更多