TS的el-tree数据处理方式,递归

javascript 复制代码
private async initData() {
    let res = await GetAllOranizationInfo()
    console.log('res数据', res)
    //获取递归方法return回来的数据
    this.treeData = this.organData(res, null)
    console.log('tree数据', this.treeData)
  }
javascript 复制代码
private organData(allData: any[], topparentId: string): TreeNodeDC[] {
    let res: TreeNodeDC[] = []
    // 通过使用filter方法,从res数据中筛选出 用户的parentId  等于 大组织id的数据 , 并将结果保存在filters数组中。
    //第一次传入的是null,匹配第一个大的组织
    let filters = allData.filter((o) => o.parentId === topparentId)
    console.log('filter之后的res,', filters)
    //对匹配上的数据进行循环操作
    for (let index = 0; index < filters.length; index++) {
      //把值放到node里形成新的对象
      const element = filters[index]
      //默认组织,因为第一个开头传的null,第一次匹配到的就是最大的组织
      let node: TreeNodeDC = {
        id: element.id,
        userId: element.userId,
        label: element.name,
        parentGroupId: element.parentId,
        index: element.index,
        children: [],
        type: 'group'
      }
      //type==0是组织(group),type==1是用户(people)
      if (element.type === 1) {
        node.type = 'people'
      }
      //递归,调用自身方法,传入 第1(n)次获取到的数据的id,然后走流程
      let nodeChildren = this.organData(allData, node.id)
      console.log('nodeChildren,', nodeChildren, allData, node.id)
      // 通过使用filter方法,从res数据中筛选出 小用户的parentId  等于 大组织id的数据 , 并将结果保存在filters数组中
      //这次的就是子级,用户+组织的形式
      //对匹配上的数据进行循环操作
      //排序
      node.children = nodeChildren.sort(function(a, b) {
        return a.index - b.index
      })
      //返回node数组
      res.push(node)
    }

    return res
  }
相关推荐
小蕾Java19 小时前
Python详细安装教程(附PyCharm使用)
开发语言·python·pycharm
简小瑞19 小时前
VSCode源码解密:一行代码解决内存泄漏难题
前端·设计模式·visual studio code
Asort19 小时前
JavaScript设计模式(九)——装饰器模式 (Decorator)
前端·javascript·设计模式
weixin_3077791319 小时前
AWS云上ClickHouse数据仓库部署方案详解
开发语言·clickhouse·自动化·云计算·aws
Man19 小时前
🔥 Vue3 动态 ref 黑科技:一招解决 v-for 中的组件引用难题!
前端·vue.js
接着奏乐接着舞。19 小时前
3D地球可视化教程 - 第3篇:地球动画与相机控制
前端·vue.js·3d·threejs
小小前端_我自坚强19 小时前
2025WebAssembly详解
前端·设计模式·前端框架
用户14125016652719 小时前
一文搞懂 Vue 3 核心原理:从响应式到编译的深度解析
前端
weixin_3077791319 小时前
使用AWS IAM和Python自动化权限策略分析与导出
开发语言·python·自动化·云计算·aws
正在走向自律19 小时前
RSA加密从原理到实践:Java后端与Vue前端全栈案例解析
java·前端·vue.js·密钥管理·rsa加密·密钥对·aes+rsa