schangxiang@126.com
2025-09-19 9be9c3784b2881a3fa25e93ae2033dc2803c0ed0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* *
* @Author: jianglei
* @Date:   2017-10-12 12:06:49
 */
"use strict";
import Vue from "vue";
export default function treeToArray(data, expandAll, parent, level, item) {
  const marLTemp = [];
  let tmp = [];
  Array.from(data).forEach(function(record) {
    if (record._expanded === undefined) {
      Vue.set(record, "_expanded", expandAll);
    }
    let _level = 1;
    if (level !== undefined && level !== null) {
      _level = level + 1;
    }
    Vue.set(record, "_level", _level);
    //  如果有父元素
    if (parent) {
      Vue.set(record, "parent", parent);
      //  如果父元素有偏移量,需要计算在this的偏移量中
      //  偏移量还与前面同级元素有关,需要加上前面所有元素的长度和
      if (!marLTemp[_level]) {
        marLTemp[_level] = 0;
      }
      Vue.set(record, "_marginLeft", marLTemp[_level] + parent._marginLeft);
      Vue.set(record, "_width", record[item] / parent[item] * parent._width);
      //  在本次计算过偏移量后加上自己长度,以供下一个元素使用
      marLTemp[_level] += record._width;
    } else {
      //  如果为根
      //  初始化偏移量存储map
      marLTemp[record.id] = [];
      //  map中是一个数组,存储的是每级的长度和
      //  初始情况下为0
      marLTemp[record.id][_level] = 0;
      Vue.set(record, "_marginLeft", 0);
      Vue.set(record, "_width", 1);
    }
    tmp.push(record);
    if (record.children && record.children.length > 0) {
      const children = treeToArray(record.children, expandAll, record, _level, item);
      tmp = tmp.concat(children);
    }
  });
  return tmp;
}