el-tree懒加载状态下实现搜索筛选(纯前端)

1.效果图

(1)初始状态
(2)筛选后

2.代码

复制代码
<template>
  <div>
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText"
      @input="searchValue"
    >
    </el-input>

    <el-tree
      class="filter-tree"
      node-key="id"
      :lazy="!openAll"
      :load="loadNode"
      :data="dataTree"
      :props="defaultProps"
      :default-expand-all="openAll"
      ref="tree"
    >
    </el-tree>
  </div>
</template>
<script>
export default {
  data() {
    return {
      openAll: false,
      filterText: '',
      dataTree: [
        {
          id: 1,
          label: 'xx公司',
          children: [
            {
              id: 3,
              label: '公关',
              children: [
                {
                  id: 6,
                  label: '张三'
                },
                {
                  id: 7,
                  label: '李四'
                }
              ]
            },
            {
              id: 8,
              label: '公关领导'
            }
          ]
        },
        {
          id: 2,
          label: 'yy娱乐',
          children: [
            {
              id: 4,
              label: '王五'
            },
            {
              id: 5,
              label: '赵六'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'label',
        isLeaf: 'isLeaf',
        id: 'id'
      },
      dataTree1: []
    }
  },
  created() {
    this.dataTree1 = JSON.parse(JSON.stringify(this.dataTree))
  },
  methods: {
    searchValue() {
      this.$nextTick(() => {
        if (
          this.filterText &&
          this.filterText !== '' &&
          this.filterText !== null
        ) {
          this.openAll = true
          //关闭懒加载
          this.$refs.tree.$data.store.lazy = false
          //全部展开
          this.$refs.tree.$data.store.defaultExpandAll = true
          //深拷贝
          let options = JSON.parse(JSON.stringify(this.dataTree))
          //清空
          this.$set(this, 'dataTree', [])
          //筛选要显示的节点
          this.searchTrees(options, this.filterText)
          //重新赋值
          this.$set(this, 'dataTree', options)
        } else {
          this.openAll = false
          this.$refs.tree.$data.store.lazy = true
          this.$refs.tree.$data.store.defaultExpandAll = false
          this.$set(this, 'dataTree', this.dataTree1)
        }
      })
    },
    //循环树筛选要显示的节点
    searchTrees(arr, val) {
      let flag = false
      if (arr && arr.length > 0) {
        for (let i = arr.length - 1; i >= 0; i--) {
          if (arr[i].children && arr[i].children.length > 0) {
            let tempflag = this.searchTrees(arr[i].children, val)
            if (tempflag == false) {
              arr.splice(i, 1)
            } else {
              flag = true
            }
          } else {
            if (arr[i].label.indexOf(val) === -1) {
              arr.splice(i, 1)
            } else {
              flag = true
            }
          }
        }
        return flag
      }
    },
    //懒加载(根据自己的数据来写)
    loadNode(node, resolve) {
      this.$nextTick(() => {
        if (this.openAll === false) {
          if (node.level === 0) {
            let topData = []
            this.dataTree.forEach(item => {
              topData.push({ label: item.label, id: item.id, isLeaf: false })
            })
            return resolve(topData)
          }
          //一级下拉
          if (node.level === 1) {
            let firstData = []
            this.dataTree.forEach(item => {
              if (item.id === node.data.id) {
                item.children.forEach(e => {
                  firstData.push({
                    label: e.label,
                    id: e.id,
                    isLeaf: e.children ? false : true
                  })
                })
              }
            })
            return resolve(firstData)
          }
          // 二级下拉
          if (node.level === 2) {
            let secondData = []
            this.dataTree.forEach(item => {
              item.children.forEach(e => {
                if (e.id === node.data.id) {
                  e.children.forEach(i => {
                    secondData.push({
                      label: i.label,
                      id: i.id,
                      isLeaf: true
                    })
                  })
                }
              })
            })
            return resolve(secondData)
          }
        }
      })
    }
  }
}
</script>
相关推荐
小贵子的博客几秒前
基于Vue3 和 Ant Design Vue实现Modal弹窗拖拽组件
前端·javascript·vue.js
小李子呢02113 分钟前
前端八股CSS---CSS选择器和优先级
前端·css
阿凤217 分钟前
uniapp如何修改下载文件位置
开发语言·前端·javascript
小李子呢02119 分钟前
前端八股---MVVM
前端·javascript·vue.js
神毓逍遥kang11 分钟前
在nest.js中我想把Java的Sa-Token搬来
前端·后端
Sheldon一蓑烟雨任平生12 分钟前
grid(一文读懂 css 网格布局)
前端·css·grid·grid-template·现代css·css 网格布局
砍材农夫19 分钟前
Hermes 搭建可视化web-dashboard界面
前端·人工智能
Z_Wonderful22 分钟前
Qiankun 子应用数据互通 + 资源共享 完整方案(React+Vue)
前端·vue.js·react.js
你的牧游哥23 分钟前
Electron核心api详解
前端·javascript·electron
渣渣xiong36 分钟前
从零开始:前端转型AI agent直到就业第十二天-第十三天
前端·人工智能