2016-08-21 86 views
0

我的JSON在不同路径中包含相同的节点名称,我希望能够打开所有具有相同名称或子名称的子项。

试过这个例子:[Search Collapsible Tree],但它只打开一条路径。
这个想法是实现搜索子字符串和如果路径有一个节点包含搜索词,然后打开(展开)该路径。

因此,我将Select2替换为文本输入,但搜索仍然限于一个结果。 enter image description hered3.js:在树形布局中展开多个路径

回答

1

你只需要改变树搜索功能找到的所有节点(然后,突出一切):

\t function searchTree(obj,search,path, paths){ 
 
\t \t if(obj.name.indexOf(search) != -1){ //if search is found return, add the object to the path and return it 
 
\t \t \t path.push(obj); 
 
\t \t \t paths.push(path.slice(0)); // clone array 
 
\t \t } 
 
\t \t else if(obj.children || obj._children){ //if children are collapsed d3 object will have them instantiated as _children 
 
\t \t \t var children = (obj.children) ? obj.children : obj._children; 
 
\t \t \t for(var i=0;i<children.length;i++){ 
 
\t \t \t \t path.push(obj);// we assume this path is the right one \t \t \t 
 
\t \t \t \t searchTree(children[i],search,path, paths); 
 
\t \t \t \t path.pop(); 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 

 
... 
 

 
\t $("#search").on("select2-selecting", function(e) { 
 
\t \t var paths = []; 
 
\t \t searchTree(root,e.object.text,[], paths); 
 
\t \t if(paths.length > 0) 
 
\t \t { 
 
\t \t \t paths.forEach(function(p) { openPaths(p) }); 
 
\t \t \t //openPaths(paths); 
 
\t \t } 
 
\t \t else{ 
 
\t \t \t alert(e.object.text+" not found!"); 
 
\t \t } 
 
\t })