Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js collapsible tree - expand/collapse nodes

I have a D3.js collapsible tree which has a bunch of nodes as like shown in below figure enter image description here

Now I want to collapse all other node if a node is expanded. For example in figure if analytics node is expanded then it should collapse data node. I am refering D3.js collapsible tree - expand/collapse intermediate nodes but its not much helpful.

like image 305
Manish Agrawal Avatar asked May 20 '26 19:05

Manish Agrawal


1 Answers

Change click function to this

function click(d) {
    for (var i = 0; i < d.parent.children.length; i++) {
        if (d.parent.children[i].name !== d.name) {

            console.log(d.parent.children[i])

            if (d.parent.children[i].children) {
                d.parent.children[i].children = d._children;
                d.parent.children[i].children = null;
            }           
        }
    };

    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    };

    update(d);
}

Hope it helps

like image 107
Przemek Avatar answered May 23 '26 09:05

Przemek