jqtree moving the childnodes

463 Views Asked by At

I want to move subchild to child nodes but I don't want to move subchild to parent node. I can move the childnode to another parent node, but it cannot be a parent node. Is jqtree better or jstree better for binding the conditions.

var data = [
    {
        label: 'node1',
        children: [
            { label: 'child1',
             children: [
                { label: 'sub-child1' }
            ]
            },
            { label: 'child2' }
        ]
    },
    {
        label: 'node2',
        children: [
            { label: 'child3' }
        ]
    }
];

$(function() {
    $('#tree1').tree({
        data: data,
        dragAndDrop: true,
        /*onCanMove: function(node) {
            if (! node.parent.parent) {
                // Example: Cannot move root node
                return false;
            } else {
                return true;
            }
        },*/
        onCanMoveTo: function(moved_node, target_node, position) {
            console.log(node);
            if (target_node == moved_node.parent.parent) {
                return (position == 'inside');                        
            }
            else {
                return true;
            }
        }
    });
});

$('#tree1').on("click",function() {
    var node = $('#tree1').tree('getSelectedNode');
    console.log(node.name);
});

fiddle

1

There are 1 best solutions below

0
On

I got the solution, if anyone is looking for a similar problem, this might help All I had to do is this

onCanMove: function(node) {
            if (!node.parent.parent) {
                return false;
            } 
            else {
                return true;
            }
        },
        onCanMoveTo: function(moved_node, target_node, position) {
           if(position == 'inside' && target_node !== moved_node.parent.parent && target_node.getLevel() < moved_node.getLevel()) {
                return true;
            } 
            else {
                return false;
            }
        }

fiddle