Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested drag and drop using jquery ui

To Drop items into dropped items

<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>

I have following code

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

How can i drop list1 item into list2 directly and list2's ul Layout tag which will come from list3 by drag and drop API of jquery?

Fiddle: http://jsfiddle.net/bhumi/nkvzdwk9/1/

like image 972
Bhumi Shah Avatar asked Apr 16 '15 08:04

Bhumi Shah


1 Answers

So what you want to achieve can be summarized like this

Step 1 : Drag and drop some Layout(which is inside li tag) from #list3 to #list2.

Step 2 : Drag and drop some media(which is also inside li tag) from #list1 to #list2 directly and also #list2 Layout's ul tag .drop-container which is now has been dragged to #list2 .

Currently , you are dropping #list1 li into #list 2 , but it should be dropped into .drop-container of #list2 or #list2(if you want to add #list li to #list2 directly)

so #list1 li should be connected to .drop-container of #list2 and #list2

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

After that, the sortable API needs to be added to .drop-container of #list2 only after #list2 has received some Layout from #list3 of

So call sortable on #list2 .drop-container inside receive method of sortable of list2. Now your receive function of #list2 becomes

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 DEMO, full code

like image 64
bugwheels94 Avatar answered Oct 27 '22 18:10

bugwheels94