Search code examples
jquerycssjquery-ui-sortablejquery-ui-draggablejquery-ui-droppable

drag and drop - sortable not working when list comes dynamically


Here i have done : http://jsfiddle.net/thilakar/QNkEL/2/

I can draggable the parent element and child element too. but i need drop the content between the child and another child(another parent child element.)

My requirement:

i need to drag and drop the text between child and also drop the content another parent child.

My Problem:

sortable not working when list comes dynamically.

Please help me..

thanks


Solution

  • I changed your javascript a little and i think i'm close to what you need:

    $(document).ready(function() {
        var treeList = "";
        var listTree = 0;
        var innerTree = 0;
    
        treeList = "<ul id=\"createTree\" class=\"droptrue\">";
        for (var key in jsonObj) {
            //alert("key: " + key + ", value: " + jsonObj[key])
            for (var skey in jsonObj[key]) {
                treeList += ("<li class=\"listTree\" id=\"asdf\">" + skey + "<ul id=\""+skey+"\">");
                for (var sskey in jsonObj[key][skey]) {
                    for (var ssskey in jsonObj[key][skey][sskey]) {
                        treeList += ("<li class=\"innerList\">" + jsonObj[key][skey][sskey][ssskey] + "</li>");
                    }
                }
                treeList += "</ul></li>";
            }
        }
        treeList += "</ul>";
    
        $('#tree').append(treeList);
    
    
        $(".listTree ul").sortable({
            revert: true
        });
    
    
        $("#Title1 li").draggable({
            helper: "clone",
            cursor: 'hand',
            revert: 'invalid',
            connectToSortable: "#Title2",
        });
    
            $("#Title2 li").draggable({
            helper: "clone",
            cursor: 'hand',
            revert: 'invalid',
            connectToSortable: "#Title1",
        });
        $( "ul, li" ).disableSelection();
    
    });
    

    fiddle here: http://jsfiddle.net/nicolapeluchetti/QNkEL/6/