我有一个D3.js可折叠树,它有一堆节点,如下图所示

现在,如果一个节点被展开,我想折叠所有其他节点。例如,在图中,如果analytics node是展开的,那么它应该折叠data node。我指的是D3.js collapsible tree - expand/collapse intermediate nodes,但它没有多大帮助。
发布于 2015-11-27 21:17:46
我在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);
}发布于 2015-11-27 15:47:07
将单击函数更改为
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);
}希望它能帮上忙
https://stackoverflow.com/questions/33960316
复制相似问题