onyxdevs / organizational-chart-with-d3-js-expandable-zoomable

An organizational chart made with D3.js with lots of features like expanding and zooming.

Home Page:https://onyxdev.net/snippets-item/organizational-chart-with-d3-js-expandable-zoomable-and-fully-initialized/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expand all node by one click

doan281 opened this issue · comments

How to expand all node by one click?

How to expand all node by one click?

Hello @doan281,

I don't remember what was going on there but now when checking the code I can recommend a place to start:

This is the expanding function:
d is the node to be toggled

if (d.children) {
    d._children = d.children;
    d.children = null;
} else {
    d.children = d._children;
    d._children = null;
}

update(d);

You should loop recursively through the tree from the root node and set all _children to children and children to null then call update on the root node.

Thanks!
I have found the solution:
`function expand(d){
if (d._children) {
d.children = d._children;
d._children = null;
}
var children = (d.children)?d.children:d._children;
if(children)
children.forEach(expand);
}

function expandAll(){
expand(root);
update(root);
}

function collapseAll(){
root.children.forEach(collapse);
collapse(root);
update(root);
}`
Ref https://stackoverflow.com/questions/28754273/d3-js-tree-expand-all-and-collapse-all