-1

我想将子子节点移动到子节点,但我不想将子子节点移动到父节点。我可以将子节点移动到另一个父节点,但它不能是父节点。是 jqtree 更好还是 jstree 更适合绑定条件。

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);
});

小提琴

4

1 回答 1

2

我得到了解决方案,如果有人正在寻找类似的问题,这可能会有所帮助我所要做的就是这个

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

小提琴

于 2015-06-23T14:23:21.917 回答