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>
相关推荐
不可求~8 小时前
多模型路由怎么落地:把任务分流写进项目的最小实现
java·前端·数据库
AC赳赳老秦11 小时前
公开图片 OCR 数据提取:OpenClaw 合规采集公开信息图,识别文字与表格并转化为结构化数据
java·大数据·前端·数据库·python·php·openclaw
谢慧琼11 小时前
2026零基础做企业网站,低成本搭建官方网站
服务器·前端·javascript
weixin_4316004412 小时前
前端数据埋点(1):一次点击如何变成一条埋点
前端·js·数据埋点
IT_陈寒12 小时前
Python线程池吞了异常还不告诉我,这谁顶得住啊
前端·人工智能·后端
计算机魔术师12 小时前
同样是AI辅助建造,为什么有的游戏三个月就烂尾了?
前端
用户9210802628613 小时前
0. 做 Agent 产品,前端 AI 组件框架怎么选?
前端
拾年27513 小时前
React Hooks 进阶篇:useMemo / useCallback / useReducer / useImperativeHandle,用「算账 +
前端·javascript·react.js
Jackson__14 小时前
面试官:如何在老项目中使用新框架,并实现通信?
前端·javascript·面试