0
我的JSON在不同路徑中包含相同的節點名稱,我希望能夠打開所有具有相同名稱或子名稱的子項。
試過這個例子:[Search Collapsible Tree],但它只打開一條路徑。
這個想法是實現搜索子字符串和如果路徑有一個節點包含搜索詞,然後打開(展開)該路徑。
因此,我將Select2替換爲文本輸入,但搜索仍然限於一個結果。 d3.js:在樹形佈局中展開多個路徑
我的JSON在不同路徑中包含相同的節點名稱,我希望能夠打開所有具有相同名稱或子名稱的子項。
試過這個例子:[Search Collapsible Tree],但它只打開一條路徑。
這個想法是實現搜索子字符串和如果路徑有一個節點包含搜索詞,然後打開(展開)該路徑。
因此,我將Select2替換爲文本輸入,但搜索仍然限於一個結果。 d3.js:在樹形佈局中展開多個路徑
你只需要改變樹搜索功能找到的所有節點(然後,突出一切):
\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 })