score:1

Accepted answer

in your click function you need to collapse all nodes, if the clicked is opened.

then open up the node you clicked like below:

function click(d) {
  //this collapse all the open nodes, if the clicked node is opened
  if(!d.children)
    data.children.foreach(collapse);
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(data);
}

working code here


Related Query