简体   繁体   中英

How do i expand specific nodes on a collapsible tree using d3.js?

我的可折叠树

I have this tree and i want to expand a single node each time. I found this code but it expands all the nodes at once.

function expand(d){
  if (d._children) {
      d.children = d._children;
      d.children.forEach(expand);
      d._children = null;
  }

}

I have a logic behind this tree and i want to expand a specific node whenever i want by passing a parameter like the node's name to choose which node i want to expand.

Either stop the recursion before messing with d._children like this:

function expand(d){
    if(d._children && d.level < 3){ // or d.name.indexOf("SpecialNode") > -1 or d.category == "expandable" or d.parent.name == "somename"  etc
        d.children = d._children;
        d.children.forEach(expand);
        d._children = null;
    }
}

or filter the d.children before forEach

function expand(d){
    if(d._children){
        d.children = d._children;
        d.children.filter(function(d) { return d.name.indexOf("SpecialNode") > -1; })
                  .forEach(expand);
        d._children = null;
    }
}

(I think I prefer the latter btw)

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM