Reputation: 43
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.
Upvotes: 1
Views: 1247
Reputation: 2718
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!
Upvotes: 2