jQuery UI Droppable and Sortable - dropping in the correct sort location

前端 未结 2 576
梦谈多话
梦谈多话 2020-12-08 16:42

I\'m working on a project where I\'m dragging elements from a 3rd party jQuery control to a jQuery sortable, using a combination of droppable and sortable.

This work

2条回答
  •  伪装坚强ぢ
    2020-12-08 16:59

    What about doing this? Using both the connectToSortable AND connectWith options works, I think. There might be a more clever way to hide/show the placeholder, but this definitely works.

    $(function () {
        $("#catalog").accordion();
        $("#catalog li").draggable({
            appendTo: "body",
            helper: "clone",
            connectToSortable: "#cart ol"
        });
        $("#cart ol").sortable({
            items: "li:not(.placeholder)",
            connectWith: "li",
            sort: function () {
    
                $(this).removeClass("ui-state-default");
            },
            over: function () {
                //hides the placeholder when the item is over the sortable
                $(".placeholder").hide(); 
    
            },
            out: function () {
                if ($(this).children(":not(.placeholder)").length == 0) {
                    //shows the placeholder again if there are no items in the list
                    $(".placeholder").show();
                }
            }
        });
    });
    

    Work Demo in Fiddle

提交回复
热议问题