使用jquery ui嵌套拖放

时间:2015-04-16 08:39:56

标签: 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>

我有以下代码

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

如何直接将list1项目放入list2,并通过拖放jquery的API将list2&#39; s ul布局标记从list3中删除?

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

1 个答案:

答案 0 :(得分:6)

所以你想要实现的目标可以像这样总结

  

第1步:将一些布局(位于li标记内)从#list3拖放到#list2

     

步骤2:拖放一些媒体(也在li标签内)   直接#list1#list2以及#list2布局的ul标记.drop-container现已被拖到#list2

目前,您要将#list1 li放入#list 2,但应将其放入.drop-container of #list2#list2(如果您要将#list li添加到#list2直接{1}}

所以#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
});

之后,只有在sortable收到来自.drop-container of #list2

的某些布局后,#list2 API才需要添加到#list3

请在sortable #list2 .drop-container的{​​{1}}内接听sortable内的list2。 现在,#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);

    }
}); 

WORKING DEMOfull code