使用jquery ui嵌套拖放

Bhu*_*hah 9 jquery jquery-ui

将项目拖放到已删除的项目中

<table id='list1' >
</table>

<ul id="list2" >
</ul>

<table id='list3' >
<tr><td>test<ul style="min-height: 100px;border:1px solid red" class="drop-container"></ul></td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我有以下代码

$( "#list3 li" ).draggable({
            connectToSortable: "#list2",
            helper: "clone",
            revert: "invalid"
});
$( "#list1 li" ).draggable({
        connectToSortable: "#list2",
        helper: "clone",
        revert: "invalid",
        greedy: true
});
Run Code Online (Sandbox Code Playgroud)

我如何直接将list1项目放入list2和list2的ul布局标签,它将通过拖放jquery的API来自list3?

小提琴:http://jsfiddle.net/bhumi/nkvzdwk9/1/

bug*_*s94 6

所以你想要实现的目标可以这样概括

第1步:将一些布局(在li标签内)拖放#list3#list2.

第2步:将一些媒体(也在li标签内) 拖放#list1#list2直接,并将#list2Layout的ul标签.drop-container拖放到现在#list2.

目前,您要删除#list1 li#list 2,但应投进.drop-container of #list2或者#list2(如果你想添加#list li#list2直接)

所以#list1 li应该连接到.drop-container of #list2#list2

$("#list1 li").draggable({
    connectToSortable: "#list2 .drop-container,#list2",//both element should be connected
    helper: "clone",
    revert: "invalid",
    greedy: true
});
Run Code Online (Sandbox Code Playgroud)

在此之后,sortableAPI需要被添加到.drop-container of #list2后,才#list2从已经收到了一些布局#list3

因此呼吁sortable#list2 .drop-container里面接受的方法sortablelist2.现在你的接收功能#list2变成了

receive: function (event, ui) {
        console.log(ui);
        console.log(event);
        var this_id = $(ui.item).attr("id");
        var preview = $(itemContext).html().replace(/<!--/g, '').replace(/-->/g, '');

        $(itemContext).attr("id", this_id);
        $(itemContext).css("width", $('#list2').width() - 20).addClass("ui-state-default").height('auto');
        $(itemContext).html(preview);

 //Modified code starts here, the sortable should be added here

        $("#list2 .drop-container").sortable({//call sortable to every element which you want to drop to.
            connectWith: "#list1",
            over: function () {
                removeIntent = false;
            },
            out: function () {
                removeIntent = true;
            },
            beforeStop: function (event, ui) {
                itemContext = ui.item.context;
                if (removeIntent == true) {
                    ui.item.remove();
                    disp($("#list2").sortable('toArray'));
                }
                //console.log(itemContext);

            },
            receive: function (event, ui) {
                console.log(ui);
                console.log(event);
                var this_id = $(ui.item).attr("id");
                var preview = $(itemContext).html().replace(/<!--/g, '').replace(/-->/g, '');

                $(itemContext).attr("id", this_id);
                $(itemContext).css("width", $('#list2').width() - 20).addClass("ui-state-default").height('auto');
                $(itemContext).html(preview);

                //console.log(this_id);
                //console.log(preview);

            }
        }); //.disableSelection()

    //end of modified code

        //console.log(this_id);
        //console.log(preview);

    }
}); 
Run Code Online (Sandbox Code Playgroud)

工作演示,完整代码