Javascript 通过项目的标题和描述过滤树视图并显示父级的算法
我有以下treeview数据:Javascript 通过项目的标题和描述过滤树视图并显示父级的算法,javascript,json,algorithm,treeview,Javascript,Json,Algorithm,Treeview,我有以下treeview数据: [ { "id":132, "parent_id":0, "title":"Item 1", "description":"", "nodes":[ { "id":133, "parent_id":132, "title":"Item 1.1", "description":"",
[
{
"id":132,
"parent_id":0,
"title":"Item 1",
"description":"",
"nodes":[
{
"id":133,
"parent_id":132,
"title":"Item 1.1",
"description":"",
"nodes":[
{
"id":134,
"parent_id":133,
"title":"Item 1.1.1",
"description":"",
"nodes":[],
}
]
}
]
},
{
"id":135,
"parent_id":0,
"title":"Item 2",
"description":"",
"nodes":[ ]
},
{
"id":136,
"parent_id":0,
"title":"Item 3",
"description":"",
"nodes":[
{
"id":137,
"parent_id":136,
"title":"Item 3.1",
"description":"",
"nodes":[ ]
}
]
}
]
我需要在所有子值中按标题和描述进行筛选,如果找到子值,则显示所有父项,以遵循层次结构
我试着用一些这样的,但这只是第一个家长的头衔:
this.visible = function (item) {
return !(this.query && this.query.length > 0 &&
item.title.indexOf(this.query) == -1);
};
像这样的怎么样
var项目=[
{
“id”:132,
“父项id”:0,
“标题”:“第1项”,
“说明”:“,
“节点”:[
{
“id”:133,
“家长id”:132,
“标题”:“第1.1项”,
“说明”:“,
“节点”:[
{
“id”:134,
“家长id”:133,
“标题”:“第1.1.1项”,
“说明”:“,
“节点”:[],
}
]
}
]
},
{
“id”:135,
“父项id”:0,
“标题”:“第2项”,
“说明”:“,
“节点”:[]
},
{
“id”:136,
“父项id”:0,
“标题”:“第3项”,
“说明”:“,
“节点”:[
{
“id”:137,
“家长id”:136,
“标题”:“第3.1项”,
“说明”:“,
“节点”:[]
}
]
}
];
函数筛选器(查询函数,项){
如果(!Array.isArray(项目)){
错误('Items不是数组!');
返回;
}
if(查询函数的类型!=“函数”){
console.error('请提供查询函数');
返回;
}
函数(项、父项){
让结果=[];
if(查询函数(项目))
结果。推送([项目,父项])
对于(设i=0;i x.title=='项目3.1',项目);
log('as:');
as.map(x=>console.log(JSON.stringify(x[0])+'\n\n父项:'+JSON.stringify(x[1]));
console.log('bs:');
console.log(JSON.stringify(bs[0][0])+'\n\n语法:'+JSON.stringify(bs[0][1]);
/*https://stackoverflow.com/users/1447675/nina-scholz */
.作为控制台包装{max height:100%!important;top:0;}
您的此