Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is the code i have tried ....

What I have tried:

function SwapInnerHtml() {
        cc = 0;
        a = [];
        serializedDataParent = [];
        serializedDataChild = [];
        var obj = $("#ContainerDIV").eq(0).clone();
        serializedData = [];
        serializedDataParent = [];
        serializedDataChild = [];
        $('#ContainerDIV').find('*').each(function () {//add class name for sortng html
            var n = $(this).attr('id');
            if (typeof (n) != 'undefined') {
                if (n.indexOf("table") != -1) {
                    if (n.indexOf("tr") == -1 && n.indexOf("td") == -1) {
                        $(this).addClass('sort');
                    }
                }
                else {
                    $(this).addClass('sort');
                }
            }
        });
        
        $('#ContainerDIV').find('.sort').each(function () { //get all elements in array.
            cc++;
            var node = document.getElementById($(this).attr('id'));
            a.push(node)
           
        });

        $('#ContainerDIV').find('.sort').each(function () {
            ///getpos($(this).attr('id'));//getting each div position with container
            var np = $(this).attr('id');
            var parent = $(this).parents().attr('id');
            // nposition = $(this).position();
            var pp = { id: np, left: nposition.left, top: nposition.top, parent: parent };
            serializedData.push(pp);
            if ($("#" + np + " .sort").length > 0) {
                serializedDataParent.push(pp);
            } else {
                var parentID = document.getElementById(np).parentNode.id
                if (parentID === 'ReadyTdrop') {
                    serializedDataParent.push(pp);
                }else{
                    serializedDataChild.push(pp);
                }
            }
        });

        serializedData.sort(function (a, b) {
            return (a.left - b.left);
        });
        serializedData.sort(function (a, b) {
            return (a.top - b.top);
        });
        serializedDataParent.sort(function (a, b) {
            return (a.left - b.left);
        });
        serializedDataParent.sort(function (a, b) {
            return (a.top - b.top);
        });
        serializedDataChild.sort(function (a, b) {
            return (a.left - b.left);
        });
        //serializedDataChild.sort(function (a, b) {
        //    return (a.top - b.top);
        //});
        $("#ContainerDIV").empty();//clear div before sorting.

        for (var i = 0; i < serializedDataParent.length; i++) {
            // var node = document.getElementById(serializedDataParent[i].id);
            for (var k = 0; k < a.length;k++)
            {
                if(a[k].id==serializedDataParent[i].id)
                {
                    var node = a[k];
                  // node.remove(".sort");
                  // node.empty();
                    $("#" + serializedDataParent[i].parent).append(node);
                    if (node.tagName!="TABLE"){
                        $("#" + a[k].id).empty();
                    }
                }
            }
         
        }
      
        for (var i = 0; i < serializedDataChild.length; i++) {
            //var node = document.getElementById(serializedDataChild[i].id);
            for (var k = 0; k < a.length; k++) {
                if (a[k].id== serializedDataChild[i].id) {
                    var node = a[k];
                    //node.remove(".sort");
                    //node.empty();
                    $("#" + serializedDataChild[i].parent).append(node);
                    if (node.tagName != "TABLE") {
                        $("#" + a[k].id).empty();
                    }
                    
                }
            }
            
        }
        

    }//swap end.


   function getpos(iid) {//get position from ReadytDrop container.
        childpos = 0;
        var parid = $("#" + iid).parent().attr('id');
        var npos_chk = $("#" + iid).position();
        if (parid != 'ContainerDIV')
            {
        $('#' + iid).parentsUntil("#ContainerDIV").each(function () {
            var idi = ($(this).attr('id'));
            var parid2 = $(this).parent().attr('id');
            var npos = $(this).position();
            npos_chk.left = npos_chk.left + npos.left;
            npos_chk.top = npos_chk.top + npos.top;
            nposition = npos_chk;
        });
        } else {
             nposition = $("#" + iid).position();
        }
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900