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

el-tree 获取过滤后的树结构

正常来说element框架应该返回的,但实际上没有,只能自己处理了

递归处理,思路就是赋值,如果是自己过滤到的数据就push进去,不是就不要

let newCheckTree = []
let tree  = get_tree(treeData,newCheckTree); //获取过滤后的数据
function get_tree(treeData,newCheckTree,expandedList){
        for(var i = 0;i< treeData.length;i++){
          if(treeData[i].child.length){
            newCheckTree[i] = {...treeData[i]}  //把所有的值赋上,但是child要为空,不然就一模一样了
            newCheckTree[i].child = []
            newCheckTree[i].child = findChildren(treeData[i].child,newCheckTree[i].child) 
          }else{
            let val = treeData[i].jGMC.toUpperCase()
            if(val.indexOf(filterText) !== -1){
              newCheckTree.push(treeData[i])
              // console.log(expandedList,'that.expandedList')
              if(expandedList.indexOf(treeData[i].sid) == -1){
                expandedList.push(treeData[i].sid)
              }
            }
           
          }
        }
        return newCheckTree
      }
      function findChildren(treeData,newCheckTree){
        for(var i = 0;i< treeData.length;i++){
          if(treeData[i].child.length){
            newCheckTree[i] = {...treeData[i]}
            newCheckTree[i].child = []
            newCheckTree[i].child = findChildren(treeData[i].child,newCheckTree[i].child) 
          }
          let val = treeData[i].jGMC.toUpperCase()
          if(val.indexOf(filterText) !== -1){
            newCheckTree.push(treeData[i])
            // console.log(expandedList,'that.expandedList')
            if(expandedList.indexOf(treeData[i].sid) == -1){
              expandedList.push(treeData[i].sid)
            }
          }
        }
        return newCheckTree
      }

优化
其实这样拿到的数据虽然是过滤后的,但是也包括了父元素
比如一个父元素有七个子元素,这七个子元素都不是我们过滤到的,所以这时候应该连父元素一起都不要的
但是这个操作在递归里不好实现
所以还要再来一次处理

let newCheckTree  = get_tree1(JSON.parse(JSON.stringify(tree))); //删掉过滤后没有子元素的数据,深拷贝不然会被影响
function get_tree1(treeData){
        for(var i = 0;i< treeData.length;i++){
          if(treeData[i]){
            if(treeData[i].child.length){
              
              treeData[i].child = findChildren1(treeData[i].child)  
            }else{
            //如果没有子数据就删掉它
              treeData.splice(i,1)
              i--
            }
          }
        }
        return treeData
      }
      function findChildren1(treeData){
        for(var i = 0;i< treeData.length;i++){
          // console.log(treeData,'treeData')
          if(treeData[i]){
            if(treeData[i].child.length){
             
            }else{
              treeData.splice(i,1)
              i--
            }
           
          }
          
        }
        return treeData
      }

相关文章:

  • 微信小程序 onLoad(option) 方法
  • CSS3 网格布局
  • laravel框架的优缺点是什么?
  • CISA 彻底改变了恶意软件信息共享:网络安全的突破
  • Linux考试复习整理
  • linux系统编程之三
  • django系列之事务操作
  • Kali Linux 安装搭建 hadoop 平台 详细教程
  • 求职半年,十月成功拿到比亚迪offer,分享一波面经....
  • Mac 使用 scp 上传或下载文件/文件夹
  • Modbus网关实现设备之间的相互通信和数据共享
  • 代码与细节(一)
  • 广东广西大量工地建筑支模
  • 京东API商品详情页,商品列表数据,商品评论数据采集
  • 2023NOIP A层联测14-选举
  • BootStrap-前端框架
  • JVM第七讲:JVM 基础 - Java 内存模型详解
  • gitlab版本库安装
  • 微信公众号粉丝迁移步骤?
  • 如何使用 MiniGPT-v2
  • 德国斯图加特发生车辆冲撞人群事件,至少三人受伤
  • 先去上海后赴北京,苏中城市泰州为何接连拥抱顶流“大城”?
  • 媒体:酒店、民宿临时毁约涨价,怎么管?
  • 华夏幸福:去年营业收入237.65亿元,同比减亏12亿元
  • 上海“模速空间”:将形成人工智能“北斗七星”和群星态势
  • 秦洪看盘|上市公司业绩“排雷”近尾声,A股下行压力趋缓