// 定义树结构,目前是写死的 实际项目中为后端接口返回
const treeData = ref([
{
name: 'bububu',
id: '0-0',
parentId: '0',
children: [
{
name: 'bbb',
id: '0-0-0',
parentId: '0-0',
children: [
{ name: 'ccc', id: '0-0-0-0', parentId: '0-0-0', },
{ name: 'aaa', id: '0-0-0-1', parentId: '0-0-0', },
],
},
{
name: 'abc',
id: '0-0-1',
parentId: '0-0',
children: [{ id: '0-0-1-0', name: 'tttt', parentId: '0-0-1', }],
},
],
},
])
const searchValue = ref('')
// 深拷贝的方法
const copyList = (arr) => {
const result = []
for (const item of arr) {
result.push(deepclone(item))
}
return result
}
const deepclone = (obj) => {
if (typeof obj === 'object') {
if (Array.isArray(obj)) {
return copyList(obj)
} else {
const result = {}
for (const key in obj) {
result[key] = deepclone(obj[key])
}
return result
}
} else {
return obj
}
}
// enter 触发搜索
const cloneArr = deepclone (treeData.value ) // 克隆数组
const onSearch = () => {
if (searchValue.value === '') {
treeData.value = cloneArr
} else {
const res = filterTree(treeData.value, searchValue.value)
treeData.value = res
}
}
// 实现方式主要为以下函数
// data是树数据 name是需要过滤的内容
function filterTree (treeData, searchValue) {
let selectIds = []
function filterId (data, searchValue) {
let flag = false
for (let i = 0; i < data.length; i++) {
const item = data[i]
if (item.name.indexOf(searchValue) > -1) {
// 放到数组的第一个
selectIds.unshift(item.id)
flag = true
} else if (item.children && item.children.length) {
if (filterId(item.children, searchValue)) {
selectIds.unshift(item.id)
flag = true
}
}
}
return flag
}
function filterTreeData (data, newData) {
filterId(treeData, searchValue)
data.forEach(item => {
if (selectIds.includes(item.id)) {
if (item.children && item.children.length) {
item.children = filterTreeData(item.children, [])
}
newData.push(item)
}
})
return newData
}
return filterTreeData(treeData, [])
}
html部分
<template>
<div>
<a-input-search v-model:value="searchValue" style="margin-bottom: 8px" placeholder="Search"
@keydown.enter="onSearch" />
<a-tree :tree-data="treeData" :fieldNames="{
children: 'children', title: 'name', key: 'id'
}">
<template #title="{ name }">
<span v-if="name.indexOf(searchValue) > -1">
{{ name.substr(0, name.indexOf(searchValue)) }}
<span style="color: #f50">{{ searchValue }}</span>
{{ name.substr(name.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ name }}</span>
</template>
</a-tree>
</div>
</template>
实现效果
1.筛选之前的数据展示
2.筛选"bb"
3.清空搜索内容
完美!!!!!!!!!!!!!!!!!!!!!