2016-08-21 5 views
0

내 JSON이 다른 경로에 동일한 노드 이름을 포함하고 있으므로 동일한 이름의 모든 하위 항목을 열거 나 이름에 하위 문자열을 포함하고 싶습니다.

시도 : [Search Collapsible Tree],이 예는 하나의 경로 만 엽니 다.
아이디어는 하위 문자열과 a에 대한 검색을 구현하는 것입니다. 경로에 검색어가 포함 된 노드가있는 경우 해당 경로를 열고 확장하십시오.

그래서 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 })