Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand all node by one click #2

Open
doan281 opened this issue Dec 19, 2023 · 2 comments
Open

Expand all node by one click #2

doan281 opened this issue Dec 19, 2023 · 2 comments

Comments

@doan281
Copy link

doan281 commented Dec 19, 2023

How to expand all node by one click?

@onyxdevs
Copy link
Owner

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.

@doan281
Copy link
Author

doan281 commented Dec 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants