Reputation: 2874
On a tree layout using d3.js (example), I'd like to collapse nodes that are not in the branch that has been clicked on.
For example, in the above demo, try the following:
Now you should see both the children of "Child 1" and "Child 2".
I would like to have the following happen:
So children of nodes other than on the "active" branch should be hidden.
How can I best approach this? (efficiently of course, as I'll be using a fairly large dataset)
Upvotes: 4
Views: 6076
Reputation: 31
if you want to collapse other nodes only when you click on nodes with children, add "&& d.children" in the second IF..
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
}
else {
d.children = d._children;
d._children = null;
}
if (d.parent && d.children) { //add here,
d.parent.children.forEach(function(element) {
if (d !== element) {
collapse(element);
}});
}
update(d);
}
Upvotes: 1
Reputation: 3902
One simple solution is to modify the click function such that if the node has a parent, the parent's children are each collapsed, but only if the child isn't the node that was clicked on.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
// If the node has a parent, then collapse its child nodes
// except for this clicked node.
if (d.parent) {
d.parent.children.forEach(function(element) {
if (d !== element) {
collapse(element);
}
});
}
update(d);
}
Updated jsbin: http://jsbin.com/etIJABU/2/edit
Upvotes: 8