我就廢話不多說了,大家還是直接看代碼吧~
ant design的table組件實現全選功能以及自定義分頁
直接附上全部代碼以及截圖了
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
|
import './index.scss' ; import React from 'react' ; import {Checkbox, Table, Popconfirm} from 'antd' ; class TestComponent extends Component { constructor (props) { super (props); this .state = { visible: false , indeterminate: true , checkAll: false , data: this .getData(), pageSize: 10 }; } state = { collapsed: false , mode: 'inline' , selectedRowKeys: [], value: undefined, }; onChange = (value) => { console.log(value); this .setState({ value }); }; onSelectChange = (selectedRowKeys) => { console.log( 'selectedRowKeys changed: ' , selectedRowKeys); this .setState({selectedRowKeys}); }; /** * 全選 * @param e */ onCheckAllChange = (e) => { const { data } = this .state; this .setState({ selectedRowKeys: e.target.checked ? data.map((item, index) => index) : [], }); }; getData = () => { const data = []; for (let i = 0; i < 8; i++) { data.push({ id: '00' +i, name: '張三' +i, age: i, address: '重慶市區...' , phone: 247839279, }); } return data; }; /** * 刪除 * @param {object} id */ handleDel = (id) => { this .setState(prevState => ({ data: prevState.data.filter(item => item.id !== id) })); }; /** * 分頁的改變 */ onShowSizeChange=(current, pageSize)=> { console.log(current, pageSize); this .setState({ pageSize: pageSize, }); } get columns () { const self = this ; return [ { title: '學號' , dataIndex: 'id' , align: 'center' , key: '1' , }, { title: '姓名' , dataIndex: 'name' , align: 'center' , key: '2' , }, { title: '年齡' , dataIndex: 'age' , align: 'center' , key: '3' , }, { title: '住址' , dataIndex: 'address' , align: 'center' , key: '4' , }, { title: '電話' , align: 'center' , dataIndex: 'phone' , key: '5' , }, { title: '操作' , align: 'center' , dataIndex: 'operation' , render(text,record) { console.log(111, record); return ( <div align= "center" > <a className= "edit-data" href= "http://localhost:8000/zh/assetDemo/info" rel= "external nofollow" >添加</a> <a href= "http://localhost:8000/zh/assetDemo/edit" rel= "external nofollow" >編輯</a> <Popconfirm title= "確定刪除?" onConfirm={() => self.handleDel(record.id)} > <span style={{cursor: 'pointer' , color: '#3f87f6' }}>刪除</span> </Popconfirm> </div> ); } } ]; } render() { const {selectedRowKeys} = this .state; const { data } = this .state; const rowSelection = { selectedRowKeys, onChange: this .onSelectChange, hideDefaultSelections: true , onSelection: this .onSelection, }; return ( <div className= "right" > <Table columns={ this .columns} dataSource={data} rowSelection={rowSelection} pagination={{ simple: false , showSizeChanger: true , showTotal: (count) => { let pageNum = Math.ceil(count / this .state.pageSize); return '共 ' + pageNum + '頁' + '/' + count + ' 條數據' ; }, onShowSizeChange: this .onShowSizeChange }} bordered /> <div className= "" > <Checkbox indeterminate={ this .state.data.length !== this .state.selectedRowKeys.length && this .state.selectedRowKeys.length !== 0} onChange={ this .onCheckAllChange} checked={ this .state.data.length === this .state.selectedRowKeys.length} >全選 </Checkbox> <span style={{cursor: 'pointer' ,color: '#3f87f6' }}> 批量刪除 </span> </div> </div> ); } } export default TestComponent; |
截圖:
補充知識:ant design pro帶分頁 自定義表格列 搜索表格組件封裝
ant design pro中有一個基礎表格的封裝,但是分頁是前端分頁處理數據;
項目中每個頁面都有用到表格,且表格都有分頁、搜索、自定義表格,所以封裝了一個定制的表格組件
實現頁面效果
組件參數
參數 | 說明 | 類型 | 默認值 |
---|---|---|---|
tablePatam | 表格的一些參數,pageSize/pageNo/loading/filterParam Object {} | ||
data | 表格數據 | array | [] |
rowKey | 頁面的唯一key | string | “” |
pathName | 頁面路徑 | String | “” |
columns | 表格的列數據 | Array | [] |
changeSearch | 改變搜索內容的方法 | function | |
onChange | 表格排序、過濾、分頁的方法調用 | function | |
handleSearch | 處理點擊搜索的方法 | function | |
handleRefresh | 點擊刷新按鈕的方法 | function | |
searchPlaceHolder | 搜索框中的placeholder內容 | String | 按名稱搜索 |
封裝的注意點
分頁
排序
搜索
頁面整個代碼
組件頁面
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
336
337
338
339
340
341
342
343
344
345
346
|
import React, { PureComponent, Fragment } from 'react' ; import { connect } from 'dva' ; import { Table, Button, Input, Checkbox, Icon, Popover, Col } from 'antd' ; import styles from './index.less' ; const { Search } = Input; function initColumns(columns) { const lists = columns.map(i => { return { show: true , ...i, }; }); return lists; } function filterColumes(columns) { const filterData = columns.filter(i => !!i.dataIndex); const initColumn = filterData.map(i => { return { dataIndex: i.dataIndex, show: i.show, }; }); return initColumn; } function saveColumns(list, path) { const str = localStorage.getItem(path); let columns = []; if (str) { const storage = JSON.parse(str); list.forEach(item => { const one = storage.find(i => i.dataIndex === item.dataIndex); const obj = { ...item, }; if (one) { obj.show = one.show; } columns.push(obj); }); } else { const simple = filterColumes(list); localStorage.setItem(path, JSON.stringify(simple)); columns = list; } return columns; } @connect(({ allspace }) => ({ allspace, })) class RefreshTable extends PureComponent { static defaultProps = { search: true , searchPlaceHolder: '按名稱模糊搜索' , save: true , scrollFlag: false , scrollY: 0, scroll: false , }; constructor(props) { super (props); this .state = { datecolumns: [], width: 0, columnVisible: false , }; } componentDidMount() { this .initData(); } componentWillReceiveProps(nextProps) { this .initData(); // todo 關于這兒是否有bug測試 // clean state const { datecolumns } = this .state; if (nextProps.columns.length > 0 && datecolumns.length > 0) { const datecolumnsRefresh = nextProps.columns.map((i, idx) => { let nowData = '' ; datecolumns.forEach(item => { if (item.dataIndex === i.dataIndex) { nowData = item; } }); const { show } = nowData; const obj = { ...nowData, ...i, show, }; return obj; }); this .setState({ datecolumns: datecolumnsRefresh, }); } } initData = () => { const { scrollFlag, columns, save, pathName } = this .props; let { width } = this .state; const initData = initColumns(columns); let datecolumns = null ; if (save) { datecolumns = saveColumns(initData, pathName); } else { datecolumns = initData; } if (scrollFlag) { datecolumns.forEach(item => { if (item.show) { width += item.width; } }); this .setState({ width, datecolumns, }); } else { this .setState({ datecolumns, }); } }; defaultList = () => { const { datecolumns = [] } = this .state; const defaultCheckedList = []; datecolumns.forEach(item => { if (item.show) { defaultCheckedList.push(item.dataIndex); } }); return defaultCheckedList; }; handleTableChange = (pagination, filters, sorter) => { const { onChange } = this .props; const { datecolumns } = this .state; if (onChange) { onChange(pagination, filters, sorter); } if (sorter.field) { datecolumns.forEach(item => { item.sortOrder = false ; item.dataIndex === sorter.field && (item.sortOrder = sorter.order); }); this .setState({ datecolumns, }); } else { datecolumns.forEach(item => { item.sortOrder = false ; }); this .setState({ datecolumns, }); } }; handleColumnVisible = () => {}; showTableColumn = visible => { this .setState({ columnVisible: visible, }); }; changeColumn = value => { const { scrollFlg, pathName } = this .props; const { datecolumns } = this .state; let width = 0; const arr = []; datecolumns.forEach(item => { const obj = { ...item, }; if (value.indexOf(item.dataIndex) !== -1) { obj.show = true ; if (scrollFlg) { width += obj.width; } } else { obj.show = false ; } arr.push(obj); }); this .setState({ datecolumns: arr, width, }); const storage = arr.map(i => { return { dataIndex: i.dataIndex, show: i.show, }; }); localStorage.setItem(pathName, JSON.stringify(storage)); }; handleCancel = () => { this .setState({ columnVisible: false , }); }; handleRefresh = () => { const { handleRefresh } = this .props; const { datecolumns } = this .state; if (handleRefresh) { datecolumns.forEach(item => { item.sortOrder = false ; }); this .setState({ datecolumns, }); handleRefresh(); } }; render() { const { scroll, scrollY, data, children, rowKey, searchPlaceHolder, tableParam, handleRefresh, handleSearch, changeSearch, pageSizeArr, searchWidth, ...rest } = this .props; const { resultList = [], totalsize = 0 } = data; const { columnVisible, datecolumns, width } = this .state; const defaultScroll = {}; if (scroll) { defaultScroll.x = width; } if (scrollY) { defaultScroll.y = scrollY; } const paginationProps = { showSizeChanger: true , showQuickJumper: true , showTotal: () => `共${totalsize}條記錄 第${tableParam.pageNo}/${Math.ceil( totalsize / tableParam.pageSize )}頁`, current: tableParam.pageNo, pageSize: tableParam.pageSize, total: totalsize, pageSizeOptions: pageSizeArr ? pageSizeArr : [ '10' , '20' , '30' , '40' ], }; const checkValue = this .defaultList(); return ( <div className={styles.RefreshTable}> <div className={styles.tableListOperator}> {handleRefresh && ( <Button icon= "reload" type= "primary" onClick={ this .handleRefresh} className={styles.tableRefresh} /> )} <Popover onVisibleChange={ this .showTableColumn} placement= "bottomLeft" visible={columnVisible} trigger= "click" className={styles.dropdown} content={ <Checkbox.Group className={styles.selsectMenu} defaultValue={checkValue} onChange={ this .changeColumn} > {datecolumns.map(item => ( <Col key={`item_${item.dataIndex}`} style={{ marginBottom: '8px' }}> <Checkbox value={item.dataIndex} key={item.dataIndex} disabled={item.disabled} className={styles.checkboxStyle} > {item.title} </Checkbox> </Col> ))} </Checkbox.Group> } > <Button className={styles.refreshButton}> <Icon type= "appstore" theme= "filled" style={{ fontSize: '14px' }} /> </Button> </Popover> {children ? <Fragment>{children}</Fragment> : null } {handleSearch && ( <Search placeholder={searchPlaceHolder} style={{ width: searchWidth ? searchWidth : '200px' , marginRight: '10px' }} value={tableParam.filterParam} onChange={changeSearch} onSearch={value => handleSearch(value)} /> )} </div> <Table {...rest} rowKey={ rowKey || (record => record.id || (record.namespace ? record.name + '/' + record.namespace : record.name)) } size= "middle" loading={tableParam.loading} dataSource={resultList} pagination={paginationProps} scroll={defaultScroll} columns={datecolumns.filter(item => item.show === true )} onChange={ this .handleTableChange} /> </div> ); } } export default RefreshTable; |
調用組件頁面
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
|
import React, { PureComponent, Fragment } from 'react' ; import { connect } from 'dva' ; import { formatMessage } from 'umi-plugin-react/locale' ; import { Card, Form, Icon, Tooltip } from 'antd' ; import RefreshTable from '@/components/RefreshTable' ; import PageHeaderWrapper from '@/components/PageHeaderWrapper' ; import SearchSelect from '@/components/SearchSelect' ; import { getAuthority } from '@/utils/authority' ; import moment from 'moment' ; @connect(({ stateless, allspace, loading }) => ({ stateless, allspace, loading: loading.models.stateless, stretchLoading: loading.effects[ 'stateless/stretch' ], })) @Form.create() class Stateless extends PureComponent { state = { pageSize: 10, pageNo: 1, filterParam: '' , sortBy: '' , sortFlag: 'desc' , namespace: '' , }; columns = [ { title: '名稱' , dataIndex: 'name' , disabled: true , sorter: true , }, { title: '命名空間' , dataIndex: 'namespace' , width: 105, textWrap: 'ellipsis' , }, { title: '更新次數' , dataIndex: 'observedGeneration' , sorter: true , width: 100, }, { title: '副本數' , dataIndex: 'replicas' , sorter: true , width: 90, }, { title: '更新副本數' , dataIndex: 'updatedReplicas' , sorter: true , width: 115, render: text => <span>{text ? text : 0}</span>, }, { title: '就緒副本' , dataIndex: 'readyReplicas' , sorter: true , width: 100, render: text => <span>{text ? text : 0}</span>, }, { title: '可用副本' , dataIndex: 'availableReplicas' , sorter: true , width: 100, render: text => <span>{text ? text : 0}</span>, }, { title: '創建時間' , dataIndex: 'createTime' , sorter: true , width: window.screen.width <= 1366 ? 95 : 155, render: val => <span>{moment(val).format( 'YYYY-MM-DD HH:mm:ss' )}</span>, }, { title: '操作' , dataIndex: 'operate' , disabled: true , width: 150, }, ]; componentDidMount() { this .getStatelessList(); } getStatelessList = value => { const { dispatch } = this .props; let params = {}; if (!value) { const { pageSize, pageNo, filterParam, sortBy, sortFlag, namespace } = this .state; params = { pageSize, pageNo, keyword: filterParam.trim(), sortBy, sortFlag, namespace, }; } else { params = value; } dispatch({ type: 'stateless/fetch' , payload: params, }); }; handleStandardTableChange = (pagination, filtersArg, sorter) => { const { filterParam, namespace } = this .state; const params = { pageNo: pagination.current, pageSize: pagination.pageSize, keyword: filterParam.trim(), namespace, }; this .setState({ pageNo: pagination.current, pageSize: pagination.pageSize, }); if (sorter.field) { params.sortBy = sorter.field; params.sortFlag = sorter.order.slice(0, -3); this .setState({ sortBy: sorter.field, sortFlag: sorter.order.slice(0, -3), }); } else { this .setState({ sortBy: '' , sortFlag: '' , }); } this .getStatelessList(params); }; handleRefresh = () => { const params = { keyword: '' , pageSize: 10, pageNo: 1, namespace: '' , }; this .setState({ filterParam: '' , pageNo: 1, pageSize: 10, namespace: '' , sortBy: '' , sortFlag: '' , }); this .getStatelessList(params); }; handleSearch = value => { const { pageSize, sortBy, sortFlag, namespace } = this .state; const params = { keyword: value.trim(), pageSize, pageNo: 1, sortBy, sortFlag, namespace, }; this .setState({ filterParam: value, pageNo: 1, }); this .getStatelessList(params); }; changeSearch = e => { this .setState({ filterParam: e.target.value, }); }; handleSpaceChange = value => { const { filterParam, sortBy, sortFlag, pageSize } = this .state; const params = { keyword: filterParam.trim(), pageSize, pageNo: 1, namespace: value === '' ? '' : value, sortBy, sortFlag, }; this .setState({ pageNo: 1, namespace: value === '' ? '' : value, }); this .getStatelessList(params); }; render() { const { stateless: { data }, loading, route, allspace, stretchLoading, } = this .props; const { filterParam, pageSize, pageNo, namespace, current = {} } = this .state; const tableParam = { pageNo, pageSize, filterParam, loading, }; const keyArr = []; if (data && data.data && data.data.resultList) { data.data.resultList .filter(item => item.message) .forEach(item => { keyArr.push(`${item.name}/${item.namespace}`); }); } return ( <PageHeaderWrapper content={`${formatMessage({ id: `statelessCaption` })}`}> <Card bordered={ false }> <RefreshTable tableParam={tableParam} data={data && data.data ? data.data : {}} rowKey={record => `${record.name}/${record.namespace}`} pathName={route.name} columns={ this .columns} changeSearch={ this .changeSearch} onChange={ this .handleStandardTableChange} handleSearch={ this .handleSearch} handleRefresh={ this .handleRefresh} expandIcon={record => CustomExpandIcon(record)} expandedRowKeys={keyArr} expandedRowRender={record => ( <Fragment> {record.message ? <span style={{ color: 'red' }}>{record.message}</span> : null } </Fragment> )} > <SearchSelect handleSpaceChange={ 'admin' .indexOf(getAuthority()) !== -1 ? this .handleSpaceChange : false } namespace={namespace} spaceData={allspace.namespace ? allspace.namespace.data.resultList : []} /> </RefreshTable> </Card> </PageHeaderWrapper> ); } } export default Stateless; |
以上這篇ant design的table組件實現全選功能以及自定義分頁就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_41140741/article/details/87907288