美文网首页
将平级数据转换为antd vue tree组件可识别的树形结构

将平级数据转换为antd vue tree组件可识别的树形结构

作者: MGLMONSTER | 来源:发表于2019-10-18 17:34 被阅读0次
    // 以下是转换为树形结构的方法  --> getRootList, getTransformItemAndChildren, getNextChildrenList, transformItem 等
    
        getRootList(list) {
    
          console.time('getrootlist')
    
          list = list || []
    
          // const rootTask = list.filter(task =>  task.upperCompanyCode == 0);
    
          const rootTask = list.find(task => task.upperCompanyCode === undefined || task.comCode === task.upperCompanyCode);
    
          if (rootTask) {
    
            const result = [this.getTransformItemAndChildren(rootTask, this.getNextChildrenList(rootTask.comCode, list))]
    
            console.timeEnd('getrootlist')
    
            return result
    
          }
    
          console.timeEnd('getrootlist')
    
          return []
    
        },
    
        getTransformItemAndChildren(task, children) {
    
          return this.transformItem(Object.assign(task, { children }))
    
        },
    
        /**
    
         * @param { Array<any> } list
    
         */
    
        getNextChildrenList: memoize(
    
          function(
    
            upperCompanyCode,
    
            list,
    
            deep = 0,
    
            filter = (task, parentcode) => task.upperCompanyCode !== task.comCode && task.upperCompanyCode === upperCompanyCode
    
          ) {
    
            list = list || []
    
            const filterChildren = []
    
            forEach(list, task => {
    
              if (filter(task, upperCompanyCode)) {
    
                filterChildren.push(
    
                  this.transformItem(
    
                    Object.assign(task, { children: deep < 5 && this.getNextChildrenList(task.comCode, list, deep + 1) })
    
                  )
    
                )
    
              }
    
            })
    
            return filterChildren
    
          },
    
          code => code
    
        ),
    
        transformItem(item, index) {
    
          // console.log('transform', item)
    
          return {
    
            ...item,
    
            key: item.comCode,
    
            title: item.comCode +'--'+ item.companyCname,
    
            value: item.comCode,
    
            children: item.children || []
    
          }
    
        },
    

    相关文章

      网友评论

          本文标题:将平级数据转换为antd vue tree组件可识别的树形结构

          本文链接:https://www.haomeiwen.com/subject/enkwmctx.html