My JSON contains same node names in different paths, I would like to be able to open all children with the same name or has substring in their names.
Tried this example: [Search Collapsible Tree https://bl.ocks.org/jjzieve/a743242f46321491a950], but it opens only one path.
The idea is to implement search for substring and a if a path has a node that contains the search term, then, open (expand) that path.
So, I replaced the Select2 to text input but the search is still limited to one result.
您只需要更改树搜索功能即可查找所有节点(然后突出显示所有内容):
function searchTree(obj,search,path, paths){
if(obj.name.indexOf(search) != -1){ //if search is found return, add the object to the path and return it
path.push(obj);
paths.push(path.slice(0)); // clone array
}
else if(obj.children || obj._children){ //if children are collapsed d3 object will have them instantiated as _children
var children = (obj.children) ? obj.children : obj._children;
for(var i=0;i<children.length;i++){
path.push(obj);// we assume this path is the right one
searchTree(children[i],search,path, paths);
path.pop();
}
}
}
...
$("#search").on("select2-selecting", function(e) {
var paths = [];
searchTree(root,e.object.text,[], paths);
if(paths.length > 0)
{
paths.forEach(function(p) { openPaths(p) });
//openPaths(paths);
}
else{
alert(e.object.text+" not found!");
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)