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
      }
相关推荐
清幽竹客43 分钟前
vue-30(理解 Nuxt.js 目录结构)
前端·javascript·vue.js
知性的小mahua44 分钟前
echarts+vue实现中国地图板块渲染+省市区跳转渲染
vue.js
weiweiweb8881 小时前
cesium加载Draco几何压缩数据
前端·javascript·vue.js
我不吃饼干9 天前
鸽了六年的某大厂面试题:你会手写一个模板引擎吗?
前端·javascript·面试
我不吃饼干9 天前
鸽了六年的某大厂面试题:手写 Vue 模板编译(解析篇)
前端·javascript·面试
前端fighter9 天前
为什么需要dependencies 与 devDependencies
前端·javascript·面试
满楼、9 天前
el-cascader 设置可以手动输入也可以下拉选择
javascript·vue.js·elementui
前端fighter9 天前
Vuex 与 Pinia:全面解析现代 Vue 状态管理的进化之路
前端·vue.js·面试
嘉琪0019 天前
2025——js 面试题
开发语言·javascript·ecmascript
snow@li9 天前
vue3-ts-qrcode :安装及使用记录 / 配置项 / 效果展示
前端·javascript·vue.js