igTree中展开特定节点时,如何展开一个节点的所有子节点?

How to expand all child nodes of a node when expanding the particular node in igTree?

我正在使用IGtree。 我在树中有数据直到树中的 4 个级别。 最初所有节点都折叠。 在扩展任何特定节点时,它的所有子节点也应该扩展到最后一层

其中一个选项是处理nodeExpanding 事件以区分根节点并找到它们的子节点是父节点。然后展开每个节点和所有节点直到根节点。这里作为代码片段:

$(document).on("igtreenodeexpanding", "#tree", function (evt, ui) {
    // this ensures the expanding node is on root level.
    if (ui.node.path.indexOf(ui.owner.options.pathSeparator) <= -1) {
        // Select all the nodes whcih contain children
        // Here you can play with the path if you want to limit expanding to a certain level
        var nodes = $(ui.node.element).find("li[data-role='node'].ui-igtree-parentnode");
        for (var i = 0; i < nodes.length ; i++) {
            var node = $(nodes[i]);
            ui.owner.expand(node);
            ui.owner.expandToNode(node);
            // or you can use: 
            // $("#tree").igTree("expand", node);
            // $("#tree").igTree("expandToNode", node);
        }
    }
});

希望对您有所帮助。

let nodes = $("#configTree").igTree("children", event.element);
  if(nodes) {
    nodes.forEach((node1)=>{
      $("#configTree").igTree("expand", node1.element);
    })
  }