现在我想在展开节点时折叠所有其他节点。例如,如果展开analytics node
,那么它应该折叠data node
。我在推荐D3.js collapsible tree - expand/collapse intermediate nodes,但它没什么用处。
答案 0 :(得分:1)
将点击功能更改为此
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);
}
希望有所帮助
答案 1 :(得分:0)
我在Przemek提示的帮助下得到了解决方案。现在它完美运作。 :)
function click(d) {
var index;
for(var i=0;i<d.parent.children.length;i++){//length of current label
if(d.parent.children[i].name===d.name)
index = i;
};
for(var i=0;i<d.parent.children.length;i++){
if(typeof d.parent.children[i].children!=="undefined" && i!=index){//if child is expnd then make null
d.parent.children[i]._children=d.parent.children[i].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);
}