There a couple fits, you just need to use regular styling to hide the markup for children (opacity: 0 or display: none). However, this simply makes the data invisible, the tree retains its shape, as if the data were there, you simply cannot see them.
Usually you need the tree to pretend that there is no data there, and update accordingly, for this you can use the same approach as the example of the layout with forced orientation in the above link.
It comes down to: 1) Use the function to build the d3 tree 2) add a click event to collapsible nodes 3) The click event renames the child property of the node and calls the function in 1), which redraws the tree without these node children.
Here is the corresponding code from the link in nkoren answer ( http://bl.ocks.org/1062288 ):
function update() { // build the tree layout here // append on("click", click) to the collapsible nodes } // Toggle children on click function click(d) { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } update(); }
forforf
source share