Blame view

src/Table/Table.js 4.03 KB
3a3ecabe   Imshann   init
1
2
3
4
5
6
7
8
9
10
11
12
  import template from "./Table.html";
  import style from "antd/lib/table/style/index.css";
  
  class Table {
      useModules = ["esNgAntd"];
  
      props = {
          columns: Array,
          dSource: Array,
          rowSelection: Object,
          rowKey: String,
          loading: Number,
dd962f77   Imshann   优化
13
          onChange: Function,
3a3ecabe   Imshann   init
14
15
16
17
18
19
20
21
      };
  
      state = {
          dataSource: [],
          selectedrecordKeys: [],
          selectedrecords: [],
          isSelectAll: false,
          rowKey: this.props.rowKey || "id",
dd962f77   Imshann   优化
22
23
24
25
26
          sortDirections: ["ascend", "descend"],
          sorter: {
              field: null,
              order: null,
          },
3a3ecabe   Imshann   init
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      };
  
      template = template;
  
      watch = {
          dSource: (newValue) => {
              if (newValue !== undefined) {
                  let dataSource = [];
                  newValue.forEach((record, index) => {
                      let row = {};
                      if (this.props.rowSelection) {
                          row.checked = false;
                          row.disabled = false;
                      }
dd962f77   Imshann   优化
41
42
43
44
45
46
47
                      if (
                          this.props.rowSelection &&
                          typeof this.props.rowSelection.getCheckboxProps ===
                              "function"
                      ) {
                          let extraAttr =
                              this.props.rowSelection.getCheckboxProps(record);
3a3ecabe   Imshann   init
48
49
50
                          row = Object.assign(row, extraAttr);
                      }
                      this.props.columns.forEach((column) => {
dd962f77   Imshann   优化
51
52
53
                          row[column.key] = column.render
                              ? column.render(record[column.key], record, index)
                              : record[column.key];
3a3ecabe   Imshann   init
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
                      });
                      // 主键
                      if (this.props.rowKey !== undefined) {
                          row[this.state.rowKey] = record[this.state.rowKey];
                      } else {
                          row[this.state.rowKey] = index + 1;
                      }
                      dataSource[index] = row;
                  });
                  this.state.dataSource = dataSource;
              }
          },
      };
  
      constructor() {
          esNgAntd.createStyle("ant-table", style);
      }
  
      handleSelectAll(event) {
          this.state.isSelectAll = event.target.checked;
          this.state.selectedrecordKeys = [];
          this.state.selectedrecords = [];
          this.state.dataSource.map((record, key) => {
              if (record.disabled === false) {
                  record.checked = event.target.checked;
              }
              if (record.checked) {
                  this.state.selectedrecordKeys.push(key);
                  this.state.selectedrecords.push(this.props.dSource[key]);
              }
              return record;
          });
dd962f77   Imshann   优化
86
87
88
89
          this.props.rowSelection.onChange(
              this.state.selectedrecordKeys,
              this.state.selectedrecords
          );
3a3ecabe   Imshann   init
90
91
92
      }
  
      handleSelect(event, index) {
dd962f77   Imshann   优化
93
94
95
          let pos = this.state.selectedrecordKeys.findIndex(
              (value) => value === index
          );
3a3ecabe   Imshann   init
96
97
98
99
100
101
102
103
104
105
          if (event.target.checked && pos === -1) {
              this.state.selectedrecordKeys.push(index);
              this.state.selectedrecords.push(this.props.dSource[index]);
          } else {
              this.state.selectedrecordKeys.splice(pos, 1);
              this.state.selectedrecords.splice(pos, 1);
          }
          if (this.state.selectedrecordKeys.length === 0) {
              this.state.isSelectAll = false;
          }
dd962f77   Imshann   优化
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
          this.props.rowSelection.onChange(
              this.state.selectedrecordKeys,
              this.state.selectedrecords
          );
      }
  
      handleSorter(key) {
          this.state.sorter.field = key;
          if (this.state.sorter.order === null) {
              this.state.sorter.order = "ascend";
          } else if (this.state.sorter.order === "ascend") {
              this.state.sorter.order = "descend"
          } else if (this.state.sorter.order === "descend") {
              this.state.sorter.order = null
              this.state.sorter.field = null;
          }
          this.props.onChange({
              sorter: this.state.sorter,
          });
3a3ecabe   Imshann   init
125
126
      }
  }