It seems you want to have a selective collapse.
That is some nodes should collapse but others should not.
To do this you need to make a JSON which indicates that this node will never collapse
Excerpts from my JSON
The one with type red is collapsible
{
"name": "cluster",
"type": "red",
"children": [{
"name": "AgglomerativeCluster",
"type": "red",
"size": 3938
},
The one with type blue is non collapsible shown below
, {
"name": "ShortestPaths",
"type": "blue",
"size": 5914
},
In your click function you will remove all other nodes except the one with type blue like shown below
function click(d) {
if (d3.event.defaultPrevented) return; // ignore drag
if (!d.collapsed) {
d.collapsed = true;
d._children = d.children;
var openNodes = [];
//on collapse remove all nodes except the one with type blue.
d.children.forEach(function (d) {
if (d.type == "blue") openNodes.push(d);
});
d.children = openNodes;
} else {
d.collapsed = false;
d.children = d._children;
d._children = null;
}
update();
}
Full working code is here
Note that in the fiddle on clicking node "cluster" all its children nodes will collapse except node "mergeEdge" because it type is blue.