Blame view

src/components/table/util.js 2.5 KB
1acabf79   梁灏   Table support mul...
1
2
  import { deepCopy } from '../../utils/assist';
  
c1e965c3   梁灏   fixed-head
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  const convertColumnOrder = (columns, fixedType) => {
      let list = [];
      let other = [];
      columns.forEach((col) => {
          if (col.fixed && col.fixed === fixedType) {
              list.push(col);
          } else {
              other.push(col);
          }
      });
      return list.concat(other);
  };
  
  export {convertColumnOrder};
  
1acabf79   梁灏   Table support mul...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  // set forTableHead to true when convertToRows, false in normal cases like table.vue
  const getAllColumns = (cols, forTableHead = false) => {
      const columns = deepCopy(cols);
      const result = [];
      columns.forEach((column) => {
          if (column.children) {
              if (forTableHead) result.push(column);
              result.push.apply(result, getAllColumns(column.children, forTableHead));
          } else {
              result.push(column);
          }
      });
      return result;
  };
  
  export {getAllColumns};
  
c1e965c3   梁灏   fixed-head
35
36
  const convertToRows = (columns, fixedType = false) => {
      const originColumns = fixedType ? fixedType === 'left' ? deepCopy(convertColumnOrder(columns, 'left')) : deepCopy(convertColumnOrder(columns, 'right')) : deepCopy(columns);
1acabf79   梁灏   Table support mul...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      let maxLevel = 1;
      const traverse = (column, parent) => {
          if (parent) {
              column.level = parent.level + 1;
              if (maxLevel < column.level) {
                  maxLevel = column.level;
              }
          }
          if (column.children) {
              let colSpan = 0;
              column.children.forEach((subColumn) => {
                  traverse(subColumn, column);
                  colSpan += subColumn.colSpan;
              });
              column.colSpan = colSpan;
          } else {
              column.colSpan = 1;
          }
      };
  
      originColumns.forEach((column) => {
          column.level = 1;
          traverse(column);
      });
  
      const rows = [];
      for (let i = 0; i < maxLevel; i++) {
          rows.push([]);
      }
  
      const allColumns = getAllColumns(originColumns, true);
  
      allColumns.forEach((column) => {
          if (!column.children) {
              column.rowSpan = maxLevel - column.level + 1;
          } else {
              column.rowSpan = 1;
          }
          rows[column.level - 1].push(column);
      });
  
      return rows;
  };
  
5d164b03   梁灏   fixed #3339
81
82
83
84
85
86
87
88
89
90
91
92
93
  export {convertToRows};
  
  const getRandomStr = function (len = 32) {
      const $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
      const maxPos = $chars.length;
      let str = '';
      for (let i = 0; i < len; i++) {
          str += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return str;
  };
  
  export {getRandomStr};