如何在删除节点时计算父节点的子节点

how to count child's of parent while dropping the node?

本文关键字:计算 父节点 子节点 节点 删除      更新时间:2023-09-26

当用户删除父节点时,我们可以在父节点的子节点的控制台中显示警告或打印吗?换句话说,当用户将任何节点掉落到另一个节点时,我想显示节点掉落到的(子节点数)的警报。

这是我的小提琴。http://jsfiddle.net/fuu94/116/

请运行fiddle扩展"c"节点。拖拽"c-a",然后拖拽"a",它会显示0(零),因为没有子节点。当它落在"b"上时,它显示2"two",因为该节点有两个子节点。

$('#tree').jstree({
    core: {
       check_callback: function (op, node, node_parent) {
          return op == 'move_node' ? node_parent.id.indexOf('not') === -1 : true;
       }
    },
    dnd: {
       is_draggable: function (x) {
          return true;
       }
    },
    "plugins": ["dnd"]
 });

似乎有一个move_node事件。但它会在下降后触发。我想我们可以假设目标节点在删除之前有n-1个子节点,因为jtree不允许将节点删除到其父节点中。

$tree = $('#tree').jstree({
    core: {
        check_callback: function(op, node, node_parent) {
            return op == 'move_node' ? node_parent.id.indexOf('not') === -1 : true;
        }
    },
    dnd: {
        is_draggable: function(x) {
            return true;
        }
    },
    "plugins": ["dnd"]
});
$tree.on('move_node.jstree', function(e, data) {
    target = $tree.jstree('get_node', data.parent, false);
    children = target.children.length-1;
    alert(children);
});