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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import React, { Component, Fragment } from 'react';
import SearchDom from '@/highOrderComponent/SearchDom';
import ButtonListDom from '../ButtonListDom';
import Shell from '@/baseComponent/Shell';
import StandardTable from '@/components/StandardTable';
import PropTypes from 'prop-types';
import { deepCopy } from '@/baseComponent/utils';
import { handleColumns } from '@/webPublic/FormInsertDiy/AffairPage/destruction';
export default class List extends Component {
constructor(props) {
super(props);
this.state = {
list: [],
selectRows: [],
formValues: this.giveDefaultValue(props),
loading: true,
sortGetPageFields: {},
pagination: {
current: 1,
total: 1,
pageSize: this.props.listConfig.pageSize || 10,
pageSizeOptions: ['10', '20', '50', '100', '500'],
showQuickJumper: true,
onShowSizeChange: (current, size) => {
this.pageChange(current, size);
},
onChange: (current, size) => {
this.pageChange(current, size);
},
},
};
}
giveDefaultValue = (props) => {
// 如果设置了 giveFieldsToFormValues 则将默认值赋值给formValues
let formValues = {};
if (
props.pageSearch &&
props.pageSearch.search &&
props.pageSearch.search.giveFieldsToFormValues
) {
for (let x in props.pageSearch.search.field) {
if (props.pageSearch.search.field[x].required === true) {
formValues[x] = props.pageSearch.search.field[x].defaultValue;
}
}
}
return formValues;
};
giveGetPageFields = () => {
const { listConfig, pageSearch } = this.props;
const { pagination, formValues } = this.state;
const search = pageSearch.search;
let data = deepCopy(formValues);
/** 如果配置了默认值 则 加进去 **/
for (let item in search.field) {
let one = search.field[item];
if (one.required && typeof data[item] === 'undefined') {
/**
* 存在必填项 且当前必填项没有值时 则传入. 如果必填项有值则不需要修改
* */
data[item] = one.defaultValue;
}
}
if (listConfig.paging) {
// 是否分页
data.pageSize = pagination.pageSize;
data.pageNo = pagination.current;
}
if (search.beforeGetPage) {
data = search.beforeGetPage(data);
}
return data;
};
getPage = () => {
const { listConfig, pageSearch, workId } = this.props;
const { pagination, sortGetPageFields } = this.state;
const { search } = pageSearch;
let data = this.giveGetPageFields();
if (!data) {
return false;
}
if (search.handleSort) {
data = {
...data,
...sortGetPageFields, // 增加排序的搜索条件
};
}
this.setState({
list: [],
loading: true,
});
search.getPageService({ ...data, appId: workId }).then((response) => {
this.setState({
loading: false,
});
if (
!response ||
(search.paging && typeof response.total === 'undefined') ||
response.errMsg
) {
return false;
}
if (listConfig.paging) {
pagination.total = Number(response.total);
}
if (typeof search.responseCallBack !== 'undefined') {
response = search.responseCallBack(response);
}
this.setState({
list: response.rows || response,
selectRows: [],
pagination: pagination,
});
});
};
stateChange = (key, value) => {
this.setState({
[key]: value,
});
};
pageChange = (current, size) => {
this.setState(
{
pagination: {
...this.state.pagination,
current: current,
pageSize: size,
},
},
() => {
this.getPage();
},
);
};
formStateChange = (value, key) => {
const { search } = this.props.pageSearch;
const { condition } = search;
let oldValue = this.state.formValues;
oldValue[key] = value;
/**
* 如果设置了beforeChange回调函数 则调用此回调函数改变另外的字段的值
* */
const index = condition.findIndex((item) => {
return item.key === key;
});
if (index !== -1) {
const thisConfig = condition[index];
if (thisConfig.beforeChange) {
oldValue = thisConfig.beforeChange(oldValue, thisConfig);
}
}
this.setState(
{
formValues: oldValue,
},
() => {
if (search.afterFormValuesChange) {
search.afterFormValuesChange(key, oldValue, this.getPage);
}
},
);
};
handleSelectRows = (rows) => {
this.setState({
selectRows: rows,
});
};
componentDidMount() {
const { search } = this.props.pageSearch;
if (!search.noNeedInitData) {
this.getPage();
}
}
componentWillMount() {}
componentWillUnmount() {}
resetFormValues = () => {
const { beforeResetFormValues } = this.props.pageSearch.search;
let { formValues } = this.state;
if (beforeResetFormValues) {
formValues = beforeResetFormValues(formValues);
} else {
formValues = {};
}
this.setState({
formValues,
});
};
/**
* 处理排序
* */
handleOnChange = (pagination, filters, sorter) => {
const { pageSearch } = this.props;
if (sorter) {
const sortGetPageFields = pageSearch.search.handleSort(sorter);
this.setState(
{
sortGetPageFields,
},
() => {
this.getPage();
},
);
}
};
render() {
const { formValues, selectRows, list, pagination, loading } = this.state;
const { listConfig, pageSearch, pageButton, children, addFields } = this.props;
let { columns, search } = pageSearch;
columns = handleColumns(columns);
let data = list;
for (let item of addFields) {
if (item.type === 'tableSelect' && item.c1) {
for (let i = 0; i < data.length; i++) {
let nameKey = item.componentProps.labelName;
if (data[i][item.c1] && data[i][item.c1].selects) {
let res = [];
for (let x in data[i][item.c1].selects) {
res.push(data[i][item.c1].selects[x][nameKey]);
}
data[i][item.c1] = res.join(',');
}
}
}
}
// console.log(columns);
const tableProps = {
rowKey: pageSearch.tableRowKey || 'id',
selectedRows: selectRows,
data: { list: data, pagination },
columns,
bordered: pageSearch.bordered || false,
loading,
noSelectRow: !listConfig.selectRows,
onSelectRow: this.handleSelectRows,
scroll: { x: window.location.href.includes('Portal/#/') ? 'max-content' : true },
selectType: 'radio',
};
// console.log(tableProps);
if (!listConfig.paging) {
tableProps.data = { list: data, pagination: false };
}
if (search && search.handleSort) {
tableProps.onChange = this.handleOnChange;
}
return (
<Fragment>
{listConfig.searchArea ? (
<SearchDom
formStateChange={this.formStateChange}
formValues={formValues}
getPage={this.getPage}
resetFormValues={this.resetFormValues}
config={pageSearch.search}
/>
) : null}
<Shell>
{listConfig.buttonArea ? (
<ButtonListDom
config={pageButton}
handleSelectRows={this.handleSelectRows}
selectRows={selectRows}
formValues={formValues}
listData={list}
search={pageSearch.search}
getPage={this.getPage}
children={children}
/>
) : null}
<StandardTable {...tableProps} />
</Shell>
</Fragment>
);
}
}
List.propTypes = {
listConfig: PropTypes.object.isRequired,
// pageButton: PropTypes.array,
pageSearch: PropTypes.object.isRequired,
};
List.defaultProps = {
listConfig: {
selectRows: true, // 是否可以行选择,
paging: true, // 是否可以分页,
searchArea: true, // 是否拥有 搜索区dom,
buttonArea: true, // 是否拥有 按钮区,
},
pageSearch: {
search: {
url: '',
field: {},
responseCallBack: (response) => {
return response;
},
condition: [
{
key: 'studentNo',
name: '学号',
type: 'input',
},
],
tableRowKey: 'id',
columns: [
{
title: '姓名',
dataIndex: '',
},
{
title: '操作',
dataIndex: 'operation',
renderType: 'update', // 修改
},
],
nameSpan: { big: 8, small: 9 },
fileSpan: { big: 4, small: 4 },
},
},
};