I\'m building a Tree using D3.js and what I am trying to do is add two buttons, Expand All and Collapse All to the top of the page like this.
Try this code. Here is the working JsFiddle.
function expand(d){
var children = (d.children)?d.children:d._children;
if (d._children) {
d.children = d._children;
d._children = null;
}
if(children)
children.forEach(expand);
}
function expandAll(){
expand(root);
update(root);
}
function collapseAll(){
root.children.forEach(collapse);
collapse(root);
update(root);
}