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

渣渣兔  金牌会员 | 2024-12-14 07:38:03 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 846|帖子 846|积分 2538

1.效果图 

(1)初始状态

 

(2)筛选后 


2.代码 

  1. <template>
  2.   <div>
  3.     <el-input
  4.       placeholder="输入关键字进行过滤"
  5.       v-model="filterText"
  6.       @input="searchValue"
  7.     >
  8.     </el-input>
  9.     <el-tree
  10.       class="filter-tree"
  11.       node-key="id"
  12.       :lazy="!openAll"
  13.       :load="loadNode"
  14.       :data="dataTree"
  15.       :props="defaultProps"
  16.       :default-expand-all="openAll"
  17.       ref="tree"
  18.     >
  19.     </el-tree>
  20.   </div>
  21. </template>
  22. <script>
  23. export default {
  24.   data() {
  25.     return {
  26.       openAll: false,
  27.       filterText: '',
  28.       dataTree: [
  29.         {
  30.           id: 1,
  31.           label: 'xx公司',
  32.           children: [
  33.             {
  34.               id: 3,
  35.               label: '公关',
  36.               children: [
  37.                 {
  38.                   id: 6,
  39.                   label: '张三'
  40.                 },
  41.                 {
  42.                   id: 7,
  43.                   label: '李四'
  44.                 }
  45.               ]
  46.             },
  47.             {
  48.               id: 8,
  49.               label: '公关领导'
  50.             }
  51.           ]
  52.         },
  53.         {
  54.           id: 2,
  55.           label: 'yy娱乐',
  56.           children: [
  57.             {
  58.               id: 4,
  59.               label: '王五'
  60.             },
  61.             {
  62.               id: 5,
  63.               label: '赵六'
  64.             }
  65.           ]
  66.         }
  67.       ],
  68.       defaultProps: {
  69.         children: 'children',
  70.         label: 'label',
  71.         isLeaf: 'isLeaf',
  72.         id: 'id'
  73.       },
  74.       dataTree1: []
  75.     }
  76.   },
  77.   created() {
  78.     this.dataTree1 = JSON.parse(JSON.stringify(this.dataTree))
  79.   },
  80.   methods: {
  81.     searchValue() {
  82.       this.$nextTick(() => {
  83.         if (
  84.           this.filterText &&
  85.           this.filterText !== '' &&
  86.           this.filterText !== null
  87.         ) {
  88.           this.openAll = true
  89.           //关闭懒加载
  90.           this.$refs.tree.$data.store.lazy = false
  91.           //全部展开
  92.           this.$refs.tree.$data.store.defaultExpandAll = true
  93.           //深拷贝
  94.           let options = JSON.parse(JSON.stringify(this.dataTree))
  95.           //清空
  96.           this.$set(this, 'dataTree', [])
  97.           //筛选要显示的节点
  98.           this.searchTrees(options, this.filterText)
  99.           //重新赋值
  100.           this.$set(this, 'dataTree', options)
  101.         } else {
  102.           this.openAll = false
  103.           this.$refs.tree.$data.store.lazy = true
  104.           this.$refs.tree.$data.store.defaultExpandAll = false
  105.           this.$set(this, 'dataTree', this.dataTree1)
  106.         }
  107.       })
  108.     },
  109.     //循环树筛选要显示的节点
  110.     searchTrees(arr, val) {
  111.       let flag = false
  112.       if (arr && arr.length > 0) {
  113.         for (let i = arr.length - 1; i >= 0; i--) {
  114.           if (arr[i].children && arr[i].children.length > 0) {
  115.             let tempflag = this.searchTrees(arr[i].children, val)
  116.             if (tempflag == false) {
  117.               arr.splice(i, 1)
  118.             } else {
  119.               flag = true
  120.             }
  121.           } else {
  122.             if (arr[i].label.indexOf(val) === -1) {
  123.               arr.splice(i, 1)
  124.             } else {
  125.               flag = true
  126.             }
  127.           }
  128.         }
  129.         return flag
  130.       }
  131.     },
  132.     //懒加载(根据自己的数据来写)
  133.     loadNode(node, resolve) {
  134.       this.$nextTick(() => {
  135.         if (this.openAll === false) {
  136.           if (node.level === 0) {
  137.             let topData = []
  138.             this.dataTree.forEach(item => {
  139.               topData.push({ label: item.label, id: item.id, isLeaf: false })
  140.             })
  141.             return resolve(topData)
  142.           }
  143.           //一级下拉
  144.           if (node.level === 1) {
  145.             let firstData = []
  146.             this.dataTree.forEach(item => {
  147.               if (item.id === node.data.id) {
  148.                 item.children.forEach(e => {
  149.                   firstData.push({
  150.                     label: e.label,
  151.                     id: e.id,
  152.                     isLeaf: e.children ? false : true
  153.                   })
  154.                 })
  155.               }
  156.             })
  157.             return resolve(firstData)
  158.           }
  159.           // 二级下拉
  160.           if (node.level === 2) {
  161.             let secondData = []
  162.             this.dataTree.forEach(item => {
  163.               item.children.forEach(e => {
  164.                 if (e.id === node.data.id) {
  165.                   e.children.forEach(i => {
  166.                     secondData.push({
  167.                       label: i.label,
  168.                       id: i.id,
  169.                       isLeaf: true
  170.                     })
  171.                   })
  172.                 }
  173.               })
  174.             })
  175.             return resolve(secondData)
  176.           }
  177.         }
  178.       })
  179.     }
  180.   }
  181. }
  182. </script>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表