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,
|
710b4ac0
Imshann
优化
|
14
|
size: String,
|
3a3ecabe
Imshann
init
|
15
16
17
|
};
state = {
|
710b4ac0
Imshann
优化
|
18
|
size: this.props.size || "default",
|
3a3ecabe
Imshann
init
|
19
20
21
22
23
|
dataSource: [],
selectedrecordKeys: [],
selectedrecords: [],
isSelectAll: false,
rowKey: this.props.rowKey || "id",
|
dd962f77
Imshann
优化
|
24
25
26
27
|
sortDirections: ["ascend", "descend"],
sorter: {
field: null,
order: null,
|
3fe10ef1
Imshann
优化
|
28
|
},
|
3a3ecabe
Imshann
init
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
};
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
优化
|
43
44
45
46
47
48
49
|
if (
this.props.rowSelection &&
typeof this.props.rowSelection.getCheckboxProps ===
"function"
) {
let extraAttr =
this.props.rowSelection.getCheckboxProps(record);
|
3a3ecabe
Imshann
init
|
50
51
52
|
row = Object.assign(row, extraAttr);
}
this.props.columns.forEach((column) => {
|
dd962f77
Imshann
优化
|
53
|
row[column.key] = column.render
|
cc35f3f0
Imshann
feat(table): 优化组件
|
54
|
? this.getRender(column, record, index)
|
dd962f77
Imshann
优化
|
55
|
: record[column.key];
|
3a3ecabe
Imshann
init
|
56
57
58
59
60
61
62
63
64
65
66
67
|
});
// 主键
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;
}
},
|
3fe10ef1
Imshann
优化
|
68
69
|
"rowSelection.selectedRowKeys": (newVal) => {
if (Array.isArray(newVal)) {
|
fe4cf8e2
Imshann
修复BUG
|
70
71
72
73
|
this.state.selectedrecordKeys = newVal;
this.state.dataSource.map(function (row, key) {
row.checked = newVal.includes(key);
});
|
3fe10ef1
Imshann
优化
|
74
75
|
}
},
|
3a3ecabe
Imshann
init
|
76
77
78
79
|
};
constructor() {
esNgAntd.createStyle("ant-table", style);
|
5b248282
Imshann
优化
|
80
81
82
83
84
85
86
|
// 初始化默认排序
this.props.columns.forEach((column) => {
if (column.sortOrder) {
this.state.sorter.field = column.key;
this.state.sorter.order = column.sortOrder;
}
});
|
3a3ecabe
Imshann
init
|
87
88
|
}
|
cc35f3f0
Imshann
feat(table): 优化组件
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
getParameterName(fn) {
if (typeof fn !== "object" && typeof fn !== "function") return;
const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
const DEFAULT_PARAMS = /=[^,)]+/gm;
const FAT_ARROWS = /=>.*$/gm;
let code = fn.prototype
? fn.prototype.constructor.toString()
: fn.toString();
code = code
.replace(COMMENTS, "")
.replace(FAT_ARROWS, "")
.replace(DEFAULT_PARAMS, "");
let result = code
.slice(code.indexOf("(") + 1, code.indexOf(")"))
.match(/([^\s,]+)/g);
return result === null ? [] : result;
}
getRender(column, record, index) {
let params = this.getParameterName(column.render);
|
45883b16
Imshann
feat(table): 优化组件
|
109
|
let render = column.render(record[column.key], record, index);
|
35f3daa2
Imshann
提交
|
110
|
if ($attrs.dSource && typeof render === "string") {
|
45883b16
Imshann
feat(table): 优化组件
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
if (params[0]) {
render = render.replace(
new RegExp(`(\{\{.*?)${params[0]}(.*?\}\})`, "g"),
`$1${$attrs.dSource}[${index}].${column.key}$2`
);
}
if (params[1]) {
render = render.replace(
new RegExp(`(\{\{.*?)${params[1]}(.*?\}\})`, "g"),
`$1${$attrs.dSource}[${index}]$2`
);
}
if (params[2]) {
render = render.replace(
new RegExp(`(\{\{.*?)${params[2]}(.*?\}\})`, "g"),
`$1${index}$2`
);
}
}
return render;
|
cc35f3f0
Imshann
feat(table): 优化组件
|
131
132
|
}
|
3a3ecabe
Imshann
init
|
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
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;
});
|
3fe10ef1
Imshann
优化
|
147
148
149
150
151
152
|
if (typeof this.props.rowSelection.onChange === "function") {
this.props.rowSelection.onChange(
this.state.selectedrecordKeys,
this.state.selectedrecords
);
}
|
3a3ecabe
Imshann
init
|
153
154
155
|
}
handleSelect(event, index) {
|
dd962f77
Imshann
优化
|
156
157
158
|
let pos = this.state.selectedrecordKeys.findIndex(
(value) => value === index
);
|
3a3ecabe
Imshann
init
|
159
160
161
162
163
164
165
166
167
168
|
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;
}
|
3fe10ef1
Imshann
优化
|
169
170
171
172
173
174
|
if (typeof this.props.rowSelection.onChange === "function") {
this.props.rowSelection.onChange(
this.state.selectedrecordKeys,
this.state.selectedrecords
);
}
|
dd962f77
Imshann
优化
|
175
176
177
178
179
180
181
|
}
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") {
|
3fe10ef1
Imshann
优化
|
182
|
this.state.sorter.order = "descend";
|
dd962f77
Imshann
优化
|
183
|
} else if (this.state.sorter.order === "descend") {
|
3fe10ef1
Imshann
优化
|
184
|
this.state.sorter.order = null;
|
dd962f77
Imshann
优化
|
185
186
187
188
189
|
this.state.sorter.field = null;
}
this.props.onChange({
sorter: this.state.sorter,
});
|
3a3ecabe
Imshann
init
|
190
191
|
}
}
|