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
  }
相关推荐
咖啡の猫25 分钟前
Vue-github 用户搜索案例
前端·vue.js·github
yong999026 分钟前
响应式布局新利器:CSS Grid 的 grid-template-areas 实战
前端·css
咖啡の猫26 分钟前
Vue过度与动画
前端·javascript·vue.js
IT_陈寒30 分钟前
Python数据处理速度慢?5行代码让你的Pandas提速300% 🚀
前端·人工智能·后端
蒜香拿铁36 分钟前
Angular【起步】
前端·javascript·angular.js
护国神蛙36 分钟前
HTTP 重定向踩坑实录:307、301、308 问题排查全指南
前端·网络协议
初心丨哈士奇37 分钟前
前端Vibe Coding探索:Cursor+MCP打造沉浸式开发流(使用MCP与Cursor Rules让Vibe Coding更快速与精准)
前端·人工智能
徐凤年lll37 分钟前
python 初学2
开发语言·python
2301_8012522244 分钟前
Mybatis的添加和修改功能
java·开发语言·mybatis
Hilaku1 小时前
前端开发,真的有必要学Docker吗?
前端·javascript·docker