2016-05-04 12 views
3

今はD3.jsツリーレイアウトを使用して組織図を作成しています。組織図はログインしたユーザーによって開かれ、デフォルトでユーザーのノードに開かれた組織図が表示されます。例えばD3.js:ノードnまでツリーを展開

は、ユーザーにログインした場合は「N」で、組織図は次のとおりです。

 j m 
    //
    b - e - k 
/
a - d - l - n 
\ 
    c- f - h 
     \ 
     i 

ユーザーが表示されます。

a - d - l - n 

ので、問題文が拡大することですOrgチャートはノードid/nameがログインしているユーザーである特定のノードまでです。

助けを歓迎します。

+0

を見たいノードでfind関数を呼び出しますか? http://stackoverflow.com/questions/3332947/when-is-it-practical-to-use-dfs-vs-bfs – 0x90

答えて

6

まず各データオブジェクトに親オブジェクトを設定します。

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)}); 
    } 
    } 

今、あなたはDFSまたはBFSについて聞いたことがありますが

var name = "layout";//example open till you find the node layout 
    find (root, name) 

作業コードhere