当前位置: 首页 > news >正文

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

1.效果图 

(1)初始状态

 

(2)筛选后 

2.代码 

<template><div><el-inputplaceholder="输入关键字进行过滤"v-model="filterText"@input="searchValue"></el-input><el-treeclass="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 = falsethis.$refs.tree.$data.store.lazy = truethis.$refs.tree.$data.store.defaultExpandAll = falsethis.$set(this, 'dataTree', this.dataTree1)}})},//循环树筛选要显示的节点searchTrees(arr, val) {let flag = falseif (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>

http://www.lryc.cn/news/416172.html

相关文章:

  • NLP——Transfromer 架构详解
  • 大模型算法面试题(二十)
  • 2024最新最全面的Selenium 3.0 + Python自动化测试框架
  • 海运中的甩柜是怎么回事❓怎么才能避免❓
  • Win11+docker+gpu+vscode+pytorch配置anomalib(2)
  • AI在招聘市场趋势分析中的应用
  • AMEYA360:太阳诱电应对 165℃的叠层金属类功率电感器实现商品化!
  • Nginx进阶-常见配置(三)
  • 开源协作式书签管理器推荐
  • 【线性代数】【二】2.2极大线性无关组与向量空间的基
  • STM32常见的下载方式有三种
  • RK3568-npu模型转换推理
  • 《C语言程序设计 第4版》笔记和代码 第十二章 数据体和数据结构基础
  • 学习记录——day26 进程间的通信 无名管道 无名管道 信号通信 特殊的信号处理
  • WHAT - xmlhttprequest vs fetch vs wretch
  • 吴恩达老师机器学习作业-ex7(聚类)
  • lombok 驼峰命名缺陷,导致后台获取参数为null的解决办法
  • 【dockerpython】亲测有效!适合新手!docker创建conda镜像+容器使用(挂载、端口映射、gpu使用)+云镜像仓库教程
  • 矩阵,求矩阵秩、逆矩阵
  • 指针和const
  • 基于C#调用文心一言大模型制作桌面软件(可改装接口)
  • VScode插件安装
  • 《Milvus Cloud向量数据库指南》——高可用黄金标准:深入解析与业务策略考量
  • sqli-labs注入练习1,2关
  • B站宋红康JAVA基础视频教程个人笔记chapter04
  • 关于java枚举为什么可以拿来当单例实现这件事看看ikun怎么说
  • Hugging Face下载模型
  • JVM-垃圾回收
  • 8.C基础_指针基础
  • 安泰功率放大器使用注意事项有哪些内容和要求