this.treeinput为当前输入款输入内容,this.treedatalist为总树的数据
if (this.treeinput) {
return this.treedatalist = this.rebuildData(this.treeinput.trim(), this.treedatalist)
} else {
this.selkgfslist()
}
方法定义
rebuildData(value, arr) {
if (!arr) {
return []
}
const newarr = [];
arr.forEach(element => {
// console.log(element)
// indexOf用来判读当前节点name字段是否包含所搜索的字符串value
// 返回值:包含则返回索引值,反之返回-1
if (element.kgfs.indexOf(value) > -1) {
const ab = this.rebuildData(value, element.children);
const obj = {
...element,
children: ab
}
newarr.push(obj);
} else {
// 判断当前节点知否有子节点,并且子节点中有数据,有数据继续递归查找
if (element.children && element.children.length > 0) {
const ab = this.rebuildData(value, element.children);
const obj = {
...element,
children: ab
};
if (ab && ab.length > 0) {
newarr.push(obj);
}
}
}
});
console.log(newarr)
return newarr
},
这样调用就能实现书序模糊搜索了