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>
相关推荐
子兮曰4 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰4 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万4 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝4 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
A黄俊辉A4 天前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
三十而立洋4 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁4 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄4 天前
JavaScript 隐式全局变量解析
javascript
honkun64 天前
vue 表格组件 vxe-table 配置 ajax 请求自动加载数据与表单查询
vue.js·vxe-table
kybs19914 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net