我现在正在使用D3.js树布局创建组织结构图.组织结构图将由登录用户打开,并且要求显示默认情况下打开到用户节点的组织结构图.
例如,如果登录用户为"n",则组织结构图为:
j m
/ /
b - e - k
/
a - d - l - n
\
c- f - h
\
i
Run Code Online (Sandbox Code Playgroud)
用户将看到:
a - d - l - n
Run Code Online (Sandbox Code Playgroud)
因此,问题陈述是将组织结构图扩展到节点id/name是登录用户的特定节点.
欢迎任何帮助.
首先在每个数据对象中设置父对象:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编写一个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)});
}
}
Run Code Online (Sandbox Code Playgroud)
现在使用您想要查看的节点调用find函数
var name = "layout";//example open till you find the node layout
find (root, name)
Run Code Online (Sandbox Code Playgroud)
在这里工作代码