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

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

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

bash 复制代码
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
      }

优化

其实这样拿到的数据虽然是过滤后的,但是也包括了父元素

比如一个父元素有七个子元素,这七个子元素都不是我们过滤到的,所以这时候应该连父元素一起都不要的

但是这个操作在递归里不好实现

所以还要再来一次处理

bash 复制代码
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
      }
相关推荐
2501_920931701 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
AI老李1 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
方也_arkling2 小时前
Element Plus主题色定制
javascript·sass
晓晓莺歌2 小时前
vue3某一个路由切换,导致所有路由页面均变成空白页
前端·vue.js
2601_949809592 小时前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
Up九五小庞2 小时前
开源埋点分析平台 ClkLog 本地部署 + Web JS 埋点测试实战--九五小庞
前端·javascript·开源
摘星编程3 小时前
React Native + OpenHarmony:UniversalLink通用链接
javascript·react native·react.js
qq_177767373 小时前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
烬头88213 小时前
React Native鸿蒙跨平台应用实现了onCategoryPress等核心函数,用于处理用户交互和状态更新,通过计算已支出和剩余预算
前端·javascript·react native·react.js·ecmascript·交互·harmonyos
程序员清洒5 小时前
Flutter for OpenHarmony:Text — 文本显示与样式控制
开发语言·javascript·flutter