前端 本地搜索 el-table 多层级-树形数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let ary = [];
let arr = [{
id: 1,
title: '你好吗?',
children: [{
id: 11,
title: '很好啊',
children: null
},
{
id: 12,
title: '是吗',
children: [{
id: 121,
title: '李世民',
children: null
},
{
id: 122,
title: '朱元璋',
children: null
}
]
}
]
},
{
id: 2,
title: '卡卡卡',
children: [{
id: 21,
title: '非常好芬',
children: null
}]
},
{
id: 22,
title: '第三方的',
children: null
}
];
const rebuildData = (value, arr) => {
if (!arr) {
return []
}
let newarr = [];
arr.forEach(element => {
if (element.title.indexOf(value) > -1) {
const ab = rebuildData(value, element.children);
const obj = {
...element,
children: ab
};
newarr.push(obj);
// console.log(newarr.push(obj), "236");
} else {
if (element.children && element.children.length > 0) {
const ab = rebuildData(value, element.children);
const obj = {
...element,
children: ab
};
if (ab && ab.length > 0) {
newarr.push(obj);
// console.log(newarr.push(obj), "789");
}
}
}
});
// console.log(newarr, "9568");
this.ary = newarr;
return newarr;
};
console.log(rebuildData('李世民', arr));
console.log(this.ary);
// console.log(arr);
</script>
</body>
</html>
打印结果-多层级表格搜索数据