zxl
2 天以前 1f233d9f5f5dafb6914fb488cd82065a8deb3e6c
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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Checkbox from "../Checkbox/Checkbox"; // eslint-disable-line
import { mixins } from "./utils";
 
/* eslint-disable no-underscore-dangle */
export default {
  name: "TreeTable__header",
  mixins: [mixins],
  data() {
    return {};
  },
  computed: {
    table() {
      return this.$parent;
    }
  },
  methods: {
    toggleAllChecked(checked) {
      this.table.bodyData = this.table.bodyData.map(row => ({
        ...row,
        _isChecked: checked
      }));
    }
  },
  render() {
    // className
    function getClassName(type, { headerAlign, key }) {
      const certainType = this.validateType(
        type,
        ["cell", "inner"],
        "getClassName"
      );
      const classList = [];
      if (key == "_normalIndex") {
        classList.push(`${this.prefixCls}--center-cell`);
      }
      if (certainType.cell) {
        classList.push(`${this.prefixCls}__header-cell`);
        if (this.table.border) {
          classList.push(`${this.prefixCls}--border-cell`);
        }
        if (["center", "right"].indexOf(headerAlign) > -1) {
          classList.push(`${this.prefixCls}--${headerAlign}-cell`);
        }
      }
      if (certainType.inner) {
        classList.push(`${this.prefixCls}__cell-inner`);
        if (this.table.treeType && this.table.firstProp === key) {
          classList.push(`${this.prefixCls}--firstProp-header-inner`);
        }
      }
      return classList.join(" ");
    }
 
    // 根据type渲染单元格Label
    function renderLabel(column, columnIndex) {
      if (
        this.isSelectionCell(this.table, columnIndex) &&
        this.selectType === "checkbox"
      ) {
        const allCheck = this.table.bodyData.every(row => row._isChecked);
        const indeterminate =
          !allCheck && this.table.bodyData.some(row => row._isChecked);
        return (
          <Checkbox
            indeterminate={indeterminate}
            value={allCheck}
            onOn-change={checked => this.toggleAllChecked(checked)}
          ></Checkbox>
        );
      }
      return column.title ? column.title : "";
    }
 
    // Template
    return (
      <table
        cellspacing="0"
        cellpadding="0"
        border="0"
        class={`${this.prefixCls}__header`}
      >
        {/* <colgroup>
          {this.table.tableColumns.map(column => (
            <col
              width={column.computedWidth || column.minWidth || column.width}
            ></col>
          ))}
        </colgroup> */}
        <thead>
          <tr class={`${this.prefixCls}__header-row`}>
            {
              this.table.tableColumns.map((column, columnIndex) =>
                column.key ? (
                  <th class={getClassName.call(this, "cell", column)}>
                    <div class={getClassName.call(this, "inner", column)}>
                      {renderLabel.call(this, column, columnIndex)}
                    </div>
                  </th>
                ) : (
                  ""
                )
              )}
          </tr>
        </thead>
      </table>
    );
  }
};