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
  }
相关推荐
沐知全栈开发1 分钟前
JavaScript 计时事件
开发语言
Yang-Never2 分钟前
Android 应用启动 -> Android 多种方式启动同一进程,Application.onCreate() 会多次执行吗?
android·java·开发语言·kotlin·android studio
plmm烟酒僧2 分钟前
《微信小程序demo开发》第一部分-编写页面逻辑
javascript·微信小程序·小程序·html·微信开发者工具·小程序开发
期待のcode3 分钟前
Java 共享变量的内存可见性问题
java·开发语言
Monly213 分钟前
【大前端】前期准备-Trae开发工具安装
前端
会游泳的石头3 分钟前
深入剖析 Java 长连接:SSE 与 WebSocket 的实战陷阱与优化策略
java·开发语言·websocket
yutian06064 分钟前
TI-C2000 系列 TMS320F2837X 控制律加速器(CLA)应用
开发语言·ti·ti c2000
2601_949720268 分钟前
flutter_for_openharmony手语学习app实战+学习进度实现
javascript·学习·flutter
夕阳之后的黑夜13 分钟前
Python脚本:为PDF批量添加水印
开发语言·python·pdf
lllljz15 分钟前
blenderGIS出现too large extent错误
java·服务器·前端