一,筛选连带父级
nodes为传入数据
query为检索键字er
filterTree(nodes, query) {
// 条件就是节点的title过滤关键字
let predicate = function(node) {
if (node.name.includes(query) || node.name.includes(query)) {
return true;
} else {
return false;
}
};
// 结束递归的条件
if (!(nodes && nodes.length)) {
return [];
}
let newChildren = [];
for (let node of nodes) {
let subs = this.filterTree(node.children, query);
if (predicate(node)) {
newChildren.push(node);
} else if (subs && subs.length) {
node.children = subs;
newChildren.push(node);
}
}
return newChildren.length ? newChildren : [];
}
二,不带父级
filterTree(nodes, query) {
// 条件就是节点的title过滤关键字
let predicate = function (node) {
if (node.name.includes(query) || node.name.includes(query)) {
return true;
} else {
return false;
}
};
// 结束递归的条件
if (!(nodes && nodes.length)) {
return [];
}
let newChildren = [];
for (let node of nodes) {
//不带父节点
if (predicate(node)) {
newChildren.push(node);
node.children = this.filterTree(node.children, query);
} else {
newChildren.push(...this.filterTree(node.children, query));
}
}
return newChildren.length ? newChildren : [];
}
改方法可以用做筛选树,树表等场景使用!