ant-design/components/table/demo/colspan-rowspan.md

138 lines
2.6 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 15
version: 4.18.0
2016-08-15 07:54:01 +08:00
title:
en-US: colSpan and rowSpan
zh-CN: 表格行/列合并
2016-03-31 09:40:55 +08:00
---
2015-10-29 15:38:59 +08:00
2016-08-15 07:54:01 +08:00
## zh-CN
2015-10-29 20:10:44 +08:00
表头只支持列合并,使用 column 里的 colSpan 进行设置。
2015-10-29 15:38:59 +08:00
2015-11-05 19:35:21 +08:00
表格支持行/列合并,使用 render 里的单元格属性 colSpan 或者 rowSpan 设值为 0 时,设置的表格不会渲染。
2015-10-29 15:38:59 +08:00
2016-08-15 08:07:03 +08:00
## en-US
Table column title supports `colSpan` that set in `column`.
Table cell supports `colSpan` and `rowSpan` that set in render return object. When each of them is set to `0`, the cell will not be rendered.
```tsx
2015-10-29 15:38:59 +08:00
import { Table } from 'antd';
import type { ColumnsType } from 'antd/lib/table';
2022-05-23 14:37:16 +08:00
import React from 'react';
interface DataType {
key: string;
name: string;
age: number;
tel: string;
phone: number;
address: string;
}
2015-10-29 15:38:59 +08:00
2016-08-15 07:54:01 +08:00
// In the fifth row, other columns are merged into first column
// by setting it's colSpan to be 0
const sharedOnCell = (_: DataType, index: number) => {
2015-10-29 16:23:09 +08:00
if (index === 4) {
return { colSpan: 0 };
2015-10-29 15:38:59 +08:00
}
return {};
2015-10-29 20:14:08 +08:00
};
2015-10-29 15:38:59 +08:00
const columns: ColumnsType<DataType> = [
2019-05-07 14:57:32 +08:00
{
title: 'Name',
dataIndex: 'name',
render: text => <a>{text}</a>,
onCell: (_, index) => ({
colSpan: (index as number) < 4 ? 1 : 5,
}),
2016-05-11 09:32:33 +08:00
},
2019-05-07 14:57:32 +08:00
{
title: 'Age',
dataIndex: 'age',
onCell: sharedOnCell,
2016-05-11 09:32:33 +08:00
},
2019-05-07 14:57:32 +08:00
{
title: 'Home phone',
colSpan: 2,
dataIndex: 'tel',
onCell: (_, index) => {
2019-05-07 14:57:32 +08:00
if (index === 2) {
return { rowSpan: 2 };
2019-05-07 14:57:32 +08:00
}
// These two are merged into above cell
if (index === 3) {
return { rowSpan: 0 };
2019-05-07 14:57:32 +08:00
}
if (index === 4) {
return { colSpan: 0 };
2019-05-07 14:57:32 +08:00
}
return {};
2019-05-07 14:57:32 +08:00
},
},
{
title: 'Phone',
colSpan: 0,
dataIndex: 'phone',
onCell: sharedOnCell,
2019-05-07 14:57:32 +08:00
},
{
title: 'Address',
dataIndex: 'address',
onCell: sharedOnCell,
2019-05-07 14:57:32 +08:00
},
];
2015-10-29 15:38:59 +08:00
const data: DataType[] = [
2019-05-07 14:57:32 +08:00
{
key: '1',
name: 'John Brown',
age: 32,
tel: '0571-22098909',
phone: 18889898989,
address: 'New York No. 1 Lake Park',
},
{
key: '2',
name: 'Jim Green',
tel: '0571-22098333',
phone: 18889898888,
age: 42,
address: 'London No. 1 Lake Park',
},
{
key: '3',
name: 'Joe Black',
age: 32,
tel: '0575-22098909',
phone: 18900010002,
address: 'Sidney No. 1 Lake Park',
},
{
key: '4',
name: 'Jim Red',
age: 18,
tel: '0575-22098909',
phone: 18900010002,
address: 'London No. 2 Lake Park',
},
{
key: '5',
name: 'Jake White',
age: 18,
tel: '0575-22098909',
phone: 18900010002,
address: 'Dublin No. 2 Lake Park',
},
];
2015-10-29 15:38:59 +08:00
const App: React.FC = () => <Table columns={columns} dataSource={data} bordered />;
export default App;
2019-05-07 14:57:32 +08:00
```