Skip to content

Commit 45a6c49

Browse files
committed
feat: support rowspan-expanded
1 parent 92dbe91 commit 45a6c49

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

docs/demo/colspan-rowspan-expanded.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: colspan-rowspan-expanded
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/colspan-rowspan-expanded.tsx"></code>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import Table from 'rc-table';
3+
import '../../assets/index.less';
4+
import type { ColumnsType } from '@/interface';
5+
6+
const columns: ColumnsType = [
7+
{
8+
title: '手机号',
9+
dataIndex: 'a',
10+
colSpan: 2,
11+
width: 100,
12+
onCell: (_, index) => {
13+
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
14+
if (index === 0) {
15+
props.rowSpan = 4;
16+
} else if (index === 1) {
17+
props.rowSpan = 0;
18+
} else if (index === 2) {
19+
props.rowSpan = 0;
20+
} else if (index === 3) {
21+
props.rowSpan = 0;
22+
} else if (index === 4) {
23+
props.rowSpan = 1;
24+
}
25+
26+
return props;
27+
},
28+
},
29+
{ title: '电话', dataIndex: 'b', colSpan: 0, width: 100 },
30+
Table.EXPAND_COLUMN,
31+
{ title: 'Name', dataIndex: 'c', width: 100 },
32+
{ title: 'Address', dataIndex: 'd', width: 200 },
33+
];
34+
35+
const data = [
36+
{ a: '13812340987', b: '0571-12345678', c: '张三', d: '文一西路', e: 'Male', key: 'a' },
37+
{ a: '13812340987', b: '0571-12345678', c: '张夫人', d: '文一西路', e: 'Female', key: 'b' },
38+
{ a: '13812340987', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: 'c' },
39+
{ a: '13812340987', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: 'd' },
40+
{ a: '1381200008888', b: '0571-099877', c: '王五', d: '文二西路', e: 'Male', key: 'e' },
41+
];
42+
43+
const Demo = () => (
44+
<div>
45+
<h2>colSpan & rowSpan & expanded</h2>
46+
<Table
47+
columns={columns}
48+
data={data}
49+
expandable={{ expandedRowRender: (record: any) => <p style={{ margin: 0 }}>{record.key}</p> }}
50+
className="table"
51+
/>
52+
</div>
53+
);
54+
55+
export default Demo;

src/Body/BodyRow.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface BodyRowProps<RecordType> {
1919
scopeCellComponent: CustomizeComponent;
2020
indent?: number;
2121
rowKey: React.Key;
22+
getRowKey: (index: number) => React.Key;
2223
}
2324

2425
// ==================================================================================
@@ -30,6 +31,7 @@ export function getCellProps<RecordType>(
3031
colIndex: number,
3132
indent: number,
3233
index: number,
34+
getRowKey: (index: number) => React.Key,
3335
) {
3436
const {
3537
record,
@@ -43,6 +45,7 @@ export function getCellProps<RecordType>(
4345
expanded,
4446
hasNestChildren,
4547
onTriggerExpand,
48+
expandedKeys,
4649
} = rowInfo;
4750

4851
const key = columnsKey[colIndex];
@@ -68,9 +71,32 @@ export function getCellProps<RecordType>(
6871
);
6972
}
7073

74+
const addChildrenRowSpan = (rowSpan: number, index2: number) => {
75+
const _index = index2 + 1;
76+
let _rowSpan = rowSpan;
77+
// 下面如果是 0 的,增加 +1 逻辑
78+
const dd = column.onCell(record, _index);
79+
if (dd.rowSpan === 0) {
80+
const ddd = expandedKeys.has(getRowKey(_index));
81+
if (ddd) {
82+
_rowSpan = _rowSpan + 1;
83+
}
84+
return addChildrenRowSpan(_rowSpan, _index);
85+
}
86+
return _rowSpan;
87+
};
88+
7189
let additionalCellProps: React.TdHTMLAttributes<HTMLElement>;
7290
if (column.onCell) {
7391
additionalCellProps = column.onCell(record, index);
92+
if (additionalCellProps.rowSpan > 0) {
93+
// 本身展开 +1
94+
if (expanded) {
95+
additionalCellProps.rowSpan = additionalCellProps.rowSpan + 1;
96+
}
97+
additionalCellProps.rowSpan = addChildrenRowSpan(additionalCellProps.rowSpan, index);
98+
}
99+
console.log('additionalCellProps.rowSpan', additionalCellProps.rowSpan);
74100
}
75101

76102
return {
@@ -102,8 +128,10 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
102128
rowComponent: RowComponent,
103129
cellComponent,
104130
scopeCellComponent,
131+
getRowKey,
105132
} = props;
106133
const rowInfo = useRowInfo(record, rowKey, index, indent);
134+
107135
const {
108136
prefixCls,
109137
flattenColumns,
@@ -144,7 +172,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
144172
)}
145173
style={{ ...style, ...rowProps?.style }}
146174
>
147-
{flattenColumns.map((column: ColumnType<RecordType>, colIndex) => {
175+
{flattenColumns.map((column, colIndex) => {
148176
const { render, dataIndex, className: columnClassName } = column;
149177

150178
const { key, fixedInfo, appendCellNode, additionalCellProps } = getCellProps(
@@ -153,8 +181,11 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
153181
colIndex,
154182
indent,
155183
index,
184+
getRowKey,
156185
);
157-
186+
if (column.title === '手机号') {
187+
// console.log('additionalCellProps', column.title, additionalCellProps);
188+
}
158189
return (
159190
<Cell<RecordType>
160191
className={columnClassName}

src/Body/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
6767
<BodyRow
6868
key={key}
6969
rowKey={key}
70+
getRowKey={index => {
71+
const thisRecord = flattenData[index]?.record;
72+
if (thisRecord) {
73+
return getRowKey(thisRecord, index);
74+
}
75+
}}
7076
record={record}
7177
index={idx}
7278
renderIndex={renderIndex}

0 commit comments

Comments
 (0)