将数据转换为树形结构


toTreeData (data) {
        var pos = {}
        var tree = []
        var i = 0
        while (data.length !== 0) {
          if (data[i].parentId === '0') {
            tree.push({
              id: data[i].id,
              label: data[i].name,
              type: data[i].type === undefined ? 0 : data[i].type.trim(),
              children: []
            })
            pos[data[i].id] = [tree.length - 1]
            data.splice(i, 1)
            i--
          } else {
            var posArr = pos[data[i].parentId]
            if (posArr !== undefined) {
              var obj = tree[posArr[0]]
              for (var j = 1; j < posArr.length; j++) {
                obj = obj.children[posArr[j]]
              }
              obj.children.push({
                id: data[i].id,
                label: data[i].name,
                type: data[i].type === undefined ? 0 : data[i].type.trim(),
                children: []
              })
              pos[data[i].id] = posArr.concat([obj.children.length - 1])
              data.splice(i, 1)
              i--
            }
          }
          i++
          if (i > data.length - 1) {
            i = 0
          }
        }
        return tree
      }

相关