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

// 以下是转换为树形结构的方法  --> 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 } 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组件可识别的树形结构)