首先設置在每個數據對象的父對象:
function collapse(d) {
if (d.children) {
d._children = d.children;
//set the parent object in all the children
d._children.forEach(function(d1){d1.parent = d; collapse(d1);});
d.children = null;
}
}
寫find
功能,找到節點,並打開所有的父母。
function find(d, name) {
if (d.name == name){
while(d.parent){
d = d.parent;
click(d);//if found open its parent
}
return;
}
//recursively call find function on its children
if (d.children) {
d.children.forEach(function(d){find(d, name)});
} else if(d._children){
d._children.forEach(function(d){find(d, name)});
}
}
現在打電話跟你想的節點查找功能看
var name = "layout";//example open till you find the node layout
find (root, name)
工作代碼here
你聽說過DFS或BFS? http://stackoverflow.com/questions/3332947/when-is-it-practical-to-use-dfs-vs-bfs – 0x90