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
      }
相关推荐
用头发抵命1 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌2 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛2 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
sp42a2 小时前
在 NativeScript-Vue 中实现流畅的共享元素转场动画
vue.js·nativescript·app 开发
柳杉2 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化
TON_G-T3 小时前
day.js和 Moment.js
开发语言·javascript·ecmascript
Irene19913 小时前
JavaScript 中 this 指向总结和箭头函数的作用域说明(附:call / apply / bind 对比总结)
javascript·this·箭头函数
2501_921930834 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-appearance(更推荐自带的Appearance)
javascript·react native·react.js
还是大剑师兰特4 小时前
Vue3 中 computed(计算属性)完整使用指南
前端·javascript·vue.js