Skip to content

Commit a0ec627

Browse files
authored
[CTable] 排序问题修复;新增 showCheckedAll 参数;[Select] 支持监听下拉选项disabled状态变化;多选下拉支持选择不限 (#719)
1 parent 6975806 commit a0ec627

17 files changed

Lines changed: 344 additions & 24 deletions

src/components/c-table/demos/checkbox.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function CTableDemo() {
4242
const [checkedData, setCheckedData] = useState([data[1], data[4], data[15]]);
4343
const [isEmpty, setIsEmpty] = useState(false);
4444
const [hideEmptyFooter, setHideEmptyFooter] = useState(false);
45+
const [showCheckedAll, setShowCheckedAll] = useState(true);
4546
const [isLoading, setIsLoading] = useState(false);
4647
const [disabled, setDisabled] = useState(false);
4748
const tableRef = useRef();
@@ -72,6 +73,9 @@ export default function CTableDemo() {
7273
<Checkbox checked={disabled} onChange={disabled => {
7374
setDisabled(disabled);
7475
}}>禁用选择</Checkbox>
76+
<Checkbox checked={showCheckedAll} onChange={checked => {
77+
setShowCheckedAll(checked);
78+
}}>显示全选当页按钮</Checkbox>
7579
</div>
7680
<div style={{ display: 'flex', alignItems: 'center', gap: 15 }}>
7781
<Button onClick={() => {
@@ -91,7 +95,7 @@ export default function CTableDemo() {
9195
</div>
9296
</div>
9397
<CTable
94-
key={String(disabled)}
98+
key={`${String(disabled)}-${String(showCheckedAll)}`}
9599
style={{ width: '100%', height: 400 }}
96100
ref={tableRef}
97101
supportExpend
@@ -107,6 +111,7 @@ export default function CTableDemo() {
107111
showRefresh={showRefresh}
108112
showTotal={showTotal}
109113
hideEmptyFooter={hideEmptyFooter}
114+
showCheckedAll={showCheckedAll}
110115
disabled={disabled}
111116
checkedData={checkedData}
112117
columnData={columns}

src/components/c-table/demos/front-table.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
```jsx
22

33
/**
4-
* title: 纯前端表格带操作
5-
* desc: 此例子用来解决:纯前端表格,对表格进行增删操作时,表格数据源异常的问题(利用对象的引用特性解决)
4+
* title: 纯前端表格增删操作
5+
* desc: 此例子用来解决:纯前端表格,对表格进行增删操作时,表格数据源异常的问题(利用对象的引用特性解决。如果有更好的解决方案欢迎戳我,我来更新文档!!!
66
*/
77
import React, { useState, useEffect } from 'react';
88
import { CTable, Button } from 'cloud-react';
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```jsx
2+
3+
/**
4+
* title: 纯前端表格带增删操作
5+
* desc: 此例子用来解决:纯前端表格,对表格进行增删操作时,表格数据源异常的问题(主要思想是在删除之后,通过改变 key 值让表格重新初始化,这种写法也有弊端,就是每次删除之后,表格滚动条无法维持位置,不过可以通过 scrollIntoView 来解决。如果有更好的解决方案欢迎戳我,我来更新文档!!!)
6+
*/
7+
import React, { useState, useEffect, createRef } from 'react';
8+
import { CTable, Button } from 'cloud-react';
9+
10+
function TableDemo({ data, setData }) {
11+
const tableRef = createRef();
12+
13+
const onDelete = row => {
14+
const targetIndex = data.findIndex(item => item.id === row.id);
15+
if (targetIndex > -1) {
16+
data.splice(targetIndex, 1);
17+
}
18+
setData([...data]);
19+
}
20+
21+
const onAdd = () => {
22+
setData([...data, { id: new Date().getTime(), name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222' }])
23+
};
24+
25+
const columns = [
26+
{ title: '活动ID', dataIndex: 'id', width: 130 },
27+
{ title: '活动名称', dataIndex: 'name', width: 140 },
28+
{ title: '创建时间', dataIndex: 'createTime', width: 140 },
29+
{ title: '人数', dataIndex: 'num', align: 'right', width: 120 },
30+
{ title: '创建人', dataIndex: 'creator', width: 130 },
31+
{
32+
title: '操作',
33+
dataIndex: 'creator',
34+
width: 100,
35+
render: (_, row) => {
36+
return (
37+
<Button type="text" onClick={() => onDelete(row)}>删除</Button>
38+
)
39+
}
40+
}
41+
];
42+
43+
return (
44+
<div>
45+
<Button style={{ marginBottom: 20 }} onClick={onAdd}>新增数据</Button>
46+
<CTable
47+
ref={tableRef}
48+
maxHeight={260}
49+
columnData={columns}
50+
ajaxData={{ totals: data.length, data }}
51+
onCell={(row, index) => {
52+
console.log(data);
53+
row.data = data;
54+
}}
55+
/>
56+
</div>
57+
);
58+
}
59+
60+
export default function CTableDemo() {
61+
const [data, setData] = useState([
62+
{ id: '121410327', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222' },
63+
]);
64+
65+
return (
66+
<div>
67+
<TableDemo key={`${data?.map(item => item.id).join(',')}`} data={data} setData={setData}/>
68+
</div>
69+
);
70+
}
71+
```

src/components/c-table/demos/sort-front.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const data = [
1919
{ id: '121410321', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' },
2020
{ id: '121410324', name: '继续发送手机4', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan', num: '12,122,112', orderNum: '112,122,112' },
2121
{ id: '121410325', name: '继续发送手机5', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo', num: '1000,000', orderNum: '200,000' },
22+
...new Array(50).fill(1).map((item, index) => (
23+
{ id: `${121410327 + index}`, name: `手机号优先继续发送${index}`, createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' }
24+
))
2225
];
2326

2427
const columns = [
@@ -71,14 +74,15 @@ export default function CTableDemo() {
7174
setSortWidthOriginStatus(checked);
7275
}}>支持排序恢复默认状态</Checkbox>
7376
<CTable
77+
style={{ height: 400 }}
7478
key={`${sortMultiColumns}${sortWidthOriginStatus}`}
7579
supportPage
7680
sortMultiColumns={sortMultiColumns}
7781
sortWidthOriginStatus={sortWidthOriginStatus}
7882
columnData={columns}
7983
ajaxData={(params) => {
8084
console.log('所有列排序配置:');
81-
console.table(params.sortParams?.allSortColumns.map(item => {
85+
console.table(params.sortParams?.allSortColumns?.map(item => {
8286
return {
8387
dataIndex: item.dataIndex,
8488
sortBy: item.sortBy
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
order: 20
3+
title: CTable
4+
desc: 表格排序
5+
---
6+
7+
```jsx
8+
9+
/**
10+
* title: 表格排序
11+
* desc: 表格排序(指定列默认排序规则:给 columnData 配置 sortBy 参数即可)
12+
*/
13+
import React from 'react';
14+
import { CTable, Tooltip, Icon } from 'cloud-react';
15+
16+
const data = new Array(50).fill(1).map((item, index) => (
17+
{ id: 121410327 + index, name: `手机号优先继续发送${index}`, createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' }
18+
))
19+
20+
const columns = [
21+
{
22+
title: '活动ID',
23+
dataIndex: 'id',
24+
sortable: true,
25+
fixed: 'left',
26+
width: 120,
27+
sortBy: 'DESC',
28+
titleTooltipConfig: {
29+
content: <span>提示信息</span>
30+
}
31+
},
32+
{
33+
title: '活动名称',
34+
dataIndex: 'name',
35+
sortable: true,
36+
width: 300
37+
},
38+
{ title: '创建时间', dataIndex: 'createTime', sortable: true, width: 200 },
39+
{
40+
title: '人数',
41+
dataIndex: 'num',
42+
align: 'right',
43+
sortable: true,
44+
width: 200,
45+
titleTooltipConfig: {
46+
content: '提示信息'
47+
},
48+
},
49+
{
50+
title: '订单数',
51+
dataIndex: 'orderNum',
52+
align: 'right',
53+
sortable: true,
54+
width: 200,
55+
titleTooltipConfig: {
56+
content: '订单数提示信息'
57+
},
58+
titleTooltipAlign: 'left',
59+
},
60+
{ title: '创建人', dataIndex: 'creator', width: 120, sortable: true, fixed: 'right' }
61+
];
62+
63+
export default function CTableDemo() {
64+
const sort = (data, { sortParams }) => {
65+
const { dataIndex, sortBy } = sortParams?.allSortColumns?.find(item => item.sortBy) || {};
66+
if (dataIndex === 'id') {
67+
return data.sort((a, b) => sortBy === 'ASC' ? Number(a.id) - Number(b.id) : Number(b.id) - Number(a.id));
68+
}
69+
if (['name', 'createTime', 'num', 'orderNum', 'creator'].includes(dataIndex)) {
70+
return data.sort((a, b) => sortBy === 'ASC' ? a[dataIndex].localeCompare(b[dataIndex]) : b[dataIndex].localeCompare(a[dataIndex]));
71+
}
72+
return data;
73+
};
74+
75+
const page = (data, { pageNum, pageSize }) => {
76+
return JSON.parse(JSON.stringify(data.slice(pageSize * (pageNum - 1), pageSize * pageNum)))
77+
}
78+
79+
return (
80+
<CTable
81+
style={{ height: 400 }}
82+
supportPage
83+
columnData={columns}
84+
ajaxData={(params) => {
85+
console.log('所有列排序配置:');
86+
console.table(params.sortParams?.allSortColumns?.map(item => {
87+
return {
88+
dataIndex: item.dataIndex,
89+
sortBy: item.sortBy
90+
}
91+
}));
92+
return new Promise(resolve => {
93+
const sortedData = sort(data, params);
94+
setTimeout(() => {
95+
resolve({ totals: sortedData.length, data: page(sortedData, params) });
96+
}, 500)
97+
})
98+
}}
99+
/>
100+
);
101+
}
102+
```

src/components/c-table/demos/sort-with-page.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ desc: 表格排序
1313
import React from 'react';
1414
import { CTable, Tooltip, Icon } from 'cloud-react';
1515

16-
const data = [
17-
{ id: '121410321', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' },
18-
{ id: '121410322', name: 'ouid疲劳度2', createTime: '2021/12/13 15:47:33 ', creator: 'jiaojiao.diao', num: '198', orderNum: '122' },
19-
{ id: '121410323', name: '继续发送手机3', createTime: '2021/12/13 15:36:42', creator: 'nan.run', num: '1,232', orderNum: '1,332' },
20-
{ id: '121410324', name: '继续发送手机4', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan', num: '12,122,112', orderNum: '112,122,112' },
21-
{ id: '121410325', name: '继续发送手机5', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo', num: '1000,000', orderNum: '200,000' },
22-
];
16+
const data = new Array(50).fill(1).map((item, index) => (
17+
{ id: 121410327 + index, name: `手机号优先继续发送${index}`, createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' }
18+
))
2319

2420
const columns = [
2521
{
@@ -79,7 +75,8 @@ export default function CTableDemo() {
7975
}
8076

8177
return (
82-
<CTable
78+
<CTable
79+
style={{ height: 400 }}
8380
supportPage
8481
columnData={columns}
8582
ajaxData={(params) => {

src/components/c-table/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CTable extends Component {
6363
selectedNodeList: this.props.checkedData,
6464
isLoading: false,
6565
filterValue: [],
66+
sortParams: {},
6667
};
6768
this.column = new Column(this);
6869
}
@@ -174,11 +175,17 @@ class CTable extends Component {
174175
childrenKey,
175176
} = this.props;
176177
this.setState({ isLoading: true }, async () => {
178+
const sortParams = {
179+
allSortColumns: [...this.state.columnData],
180+
};
177181
const res = await this.getDataSource(ajaxData, {
178182
...pageOpts,
179183
filterValue,
184+
sortParams,
180185
});
181186

187+
this.setState({ sortParams });
188+
182189
if (childrenKey !== 'children') {
183190
traverseTree({
184191
tree: res[dataKey],
@@ -492,6 +499,8 @@ class CTable extends Component {
492499
filterValue,
493500
};
494501

502+
this.setState({ sortParams });
503+
495504
const { ajaxData, dataKey, childrenKey } = this.props;
496505
const res = await this.getDataSource(ajaxData, params);
497506
if (childrenKey !== 'children') {
@@ -554,7 +563,7 @@ class CTable extends Component {
554563
if (pageOpts && pageOpts.onChange) {
555564
pageOpts.onChange({ pageNum, pageSize });
556565
}
557-
this.loadGrid({ pageNum, pageSize });
566+
this.loadGrid({ pageNum, pageSize, sortParams: this.state.sortParams });
558567
};
559568

560569
/**

src/components/c-table/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ this.tableRef.current.setColumn(columnData, isReloadGrid?);
8888
| onCheckedAllAfter | 选中当页回调,需要设置 supportCheckbox 为 true,Function(checkedList, currentPageData, checked) | function | - | |
8989
| showFooterSelect | 配置是否显示已选条数 | boolean | true | |
9090
| disabled | 禁用多选/单选按钮 | boolean | false | |
91+
| showCheckedAll | 是否展示表格全选当页按钮 | boolean | true | |
9192

9293
#### CTable 表格拖拽配置
9394
| 属性 | 说明 | 类型 | 默认值
@@ -165,6 +166,7 @@ this.tableRef.current.setColumn(columnData, isReloadGrid?);
165166
| render | 自定义列模板 | function | - |
166167
| sortable | 是否支持排序 | boolean | false |
167168
| sorter | 自定义列排序规则 | function | - |
169+
| sortBy | 指定列默认排序规则(正序/倒叙)`ASC` `DESC` | string | '' |
168170
| onCell | 为每个单元格设置自定义参数 Function(record, index) | function | - |
169171
| minWidth | 列最小宽度(**该属性效果不流畅,可以给 columnData 中的每一项都设置 width 属性,可达到同样效果**| number | - |
170172
| filters | 配置表格列筛选项 [{ text: '男', value: 'male' }, { text: '女', value: 'female' }] | array | [] |
@@ -247,6 +249,8 @@ this.tableRef.current.setColumn(columnData, isReloadGrid?);
247249

248250
<embed src="@components/c-table/demos/sort-front.md" />
249251

252+
<embed src="@components/c-table/demos/sort-store.md" />
253+
250254
### 表格过滤
251255

252256
<embed src="@components/c-table/demos/filter.md" />
@@ -285,4 +289,6 @@ this.tableRef.current.setColumn(columnData, isReloadGrid?);
285289

286290
<embed src="@components/c-table/demos/front-table.md" />
287291

292+
<embed src="@components/c-table/demos/front-table1.md" />
293+
288294
<embed src="@components/c-table/demos/table-in-tab.md" />

src/components/c-table/js/column.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export default class Column {
416416
getCheckboxColumn = (isFirstColumnFixed) => {
417417
const { _this } = this;
418418
const { leafNodesMap } = _this;
419-
const { disabled } = _this.props;
419+
const { disabled, showCheckedAll } = _this.props;
420420

421421
const currentLeafNodes = Object.keys(leafNodesMap).reduce(
422422
(nodeList, key) => {
@@ -433,14 +433,16 @@ export default class Column {
433433
const isIndeterminateAll = !isCheckedAll && isSomeChecked(currentLeafNodes);
434434

435435
return {
436-
title: (
436+
title: showCheckedAll ? (
437437
<Checkbox
438438
style={{ float: 'left' }}
439439
disabled={disabled}
440440
checked={isCheckedAll}
441441
indeterminate={isIndeterminateAll}
442442
onChange={(checked) => this.onAllCheckedChange(checked)}
443443
/>
444+
) : (
445+
''
444446
),
445447
className: `${tablePrefixCls}-checkbox-column`,
446448
dataIndex: 'checkbox',

src/components/c-table/js/propType.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const propTypes = {
7272
stickyFooter: PropTypes.bool,
7373
sortWidthOriginStatus: PropTypes.bool,
7474
sortMultiColumns: PropTypes.bool,
75+
showCheckedAll: PropTypes.bool,
7576
};
7677

7778
export const defaultProps = {
@@ -143,4 +144,5 @@ export const defaultProps = {
143144
stickyFooter: false,
144145
sortWidthOriginStatus: false,
145146
sortMultiColumns: false,
147+
showCheckedAll: true,
146148
};

0 commit comments

Comments
 (0)