-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathcolumn-resize.tsx
106 lines (100 loc) · 3.63 KB
/
column-resize.tsx
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
import React, { useState } from 'react';
import Table, { INTERNAL_HOOKS } from 'rc-table';
import type { ColumnType } from 'rc-table';
import '../../assets/index.less';
const data = [
{ a: '123', b: 'xxxxxxxx xxxxxxxx', d: 3, key: '1' },
{ a: 'cdd', b: 'edd12221 edd12221', d: 3, key: '2' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '3' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '4' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '5' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '6' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '7' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '8' },
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '9' },
];
const Demo = () => {
const [widthMap, setWidthMap] = useState<Map<React.Key, number>>(new Map());
const columns1 = [
{ title: 'title1', dataIndex: 'aaa', key: 'aaa', width: 100 },
{ title: 'title2', dataIndex: 'bbb', key: 'bbb', width: 100 },
].map(i => ({
...i,
resizable: true,
width: widthMap.get(i.key ?? i.dataIndex) ?? i.width,
})) as ColumnType<any>[];
const columns2 = [
{ title: 'title1', dataIndex: 'a', key: 'a', fixed: 'left' },
{ title: 'title2', dataIndex: 'b', key: 'b', fixed: 'left' },
{ title: 'title3', dataIndex: 'c', key: 'c' },
{ title: 'title4', dataIndex: 'b', key: 'd' },
{ title: 'title5', dataIndex: 'b', key: 'e' },
{ title: 'title6', dataIndex: 'b', key: 'f' },
{ title: 'title7', dataIndex: 'b', key: 'g' },
{ title: 'title8', dataIndex: 'b', key: 'h' },
{ title: 'title9', dataIndex: 'b', key: 'i' },
{ title: 'title10', dataIndex: 'b', key: 'j' },
{ title: 'title11', dataIndex: 'b', key: 'k', fixed: 'right' },
{ title: 'title12', dataIndex: 'b', key: 'l', fixed: 'right' },
].map(i => ({
...i,
resizable: true,
width: widthMap.get(i.key ?? i.dataIndex) ?? 150,
})) as ColumnType<any>[];
return (
<div>
table width: 800px {'columns=[{width: 100, width: 100}]'} 情况
<Table
style={{ width: 800 }}
scroll={{ y: 300, x: columns1.reduce((t, c) => t + (c.width as number), 0) }}
columns={columns1}
data={data}
onColumnResizeComplete={({ columnWidths }) => {
setWidthMap(prev => {
const result = new Map(prev);
columnWidths.forEach(i => {
result.set(i.columnKey, i.width);
});
return result;
});
}}
internalHooks={INTERNAL_HOOKS}
getContainerWidth={(ele, width) => {
// Minus border
const borderWidth = getComputedStyle(
ele.querySelector('.rc-table-body'),
).borderInlineStartWidth;
const mergedWidth = width - parseInt(borderWidth, 10);
return mergedWidth;
}}
/>
<br />
大多数情况
<Table
style={{ width: 800 }}
scroll={{ y: 300, x: columns2.reduce((t, c) => t + (c.width as number), 0) }}
columns={columns2}
data={data}
onColumnResizeComplete={({ columnWidths }) => {
setWidthMap(prev => {
const result = new Map(prev);
columnWidths.forEach(i => {
result.set(i.columnKey, i.width);
});
return result;
});
}}
internalHooks={INTERNAL_HOOKS}
getContainerWidth={(ele, width) => {
// Minus border
const borderWidth = getComputedStyle(
ele.querySelector('.rc-table-body'),
).borderInlineStartWidth;
const mergedWidth = width - parseInt(borderWidth, 10);
return mergedWidth;
}}
/>
</div>
);
};
export default Demo;