2017-04-21 153 views
1

My collapsible tree如何使用d3.js展开可折叠树上的特定节点?

我有这棵树,我想每次展开一个节点。我发现了这个代码,但它一次展开所有节点。

function expand(d){ 
    if (d._children) { 
     d.children = d._children; 
     d.children.forEach(expand); 
     d._children = null; 
    } 

}

我有这棵树背后的逻辑,我想展开一个特定节点每当我想通过传递一个参数,如节点的名称,选择我要扩大哪个节点。与d._children搞乱像在此之前

回答

2

要么停止递归:

function expand(d){ 
    if(d._children && d.level < 3){ // or d.name.indexOf("SpecialNode") > -1 or d.category == "expandable" or d.parent.name == "somename" etc 
     d.children = d._children; 
     d.children.forEach(expand); 
     d._children = null; 
    } 
} 

或过滤d.childrenforEach

function expand(d){ 
    if(d._children){ 
     d.children = d._children; 
     d.children.filter(function(d) { return d.name.indexOf("SpecialNode") > -1; }) 
        .forEach(expand); 
     d._children = null; 
    } 
} 

(我想我更喜欢者均基于后者)

好运!