You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.4 KiB

# 表格行/列合并
- order: 13
9 years ago
表头只支持列合并,cell = [{title: '姓名', `colSpan: 2`}]。
表格支持行/列合并,可以指定内容对齐方式,colSpan或者rowSpan设值为 0 时,设置的表格不会渲染。
---
````jsx
import { Table } from 'antd';
9 years ago
// 事例表中第四行合并了五列,除了第一列设置colSpan=5 外,其他列的第四行colSpan=0 (被合并掉,不会渲染)
const renderContent = function(value, row, index) {
let obj = {
children: value,
9 years ago
props: {}
}
9 years ago
if (index === 4) {
obj.props.colSpan = 0;
}
return obj;
}
const columns = [{
title: '姓名',
dataIndex: 'name',
render: function(text, row, index) {
9 years ago
if (index < 4) {
return <a href="javascript:;">{text}</a>;
}else{
return {
children: <a href="javascript:;">{text}</a>,
9 years ago
props: {
align: 'center',
colSpan: 5
}
}
}
}
9 years ago
}, {
title: '年龄',
dataIndex: 'age',
render: renderContent
9 years ago
}, {
title: '家庭电话',
colSpan: 2,
dataIndex: 'tel',
9 years ago
render: function(value, row, index) {
let obj = {
children: value,
props:{}
}
9 years ago
// 第三列的第三行行合并
if (index === 2) {
obj.props.rowSpan = 2;
}
9 years ago
// 第三列的第四行被合并没了,设置rowSpan=0 直接不用渲染
if (index === 3) {
obj.props.rowSpan = 0;
}
9 years ago
if (index === 4) {
obj.props.colSpan = 0;
}
return obj;
}
9 years ago
}, {
title: '手机号',
colSpan: 0,
dataIndex: 'phone',
render: renderContent
9 years ago
}, {
title: '住址',
dataIndex: 'address',
render: renderContent
}];
const data = [{
key: '1',
name: '胡彦斌',
age: 32,
tel: '0571-22098909',
phone: 18889898989,
address: '西湖区湖底公园1号'
}, {
key: '2',
name: '胡彦祖',
tel: '0571-22098333',
phone: 18889898888,
age: 42,
address: '西湖区湖底公园1号'
}, {
key: '3',
name: '李大嘴',
age: 32,
tel: '0575-22098909',
phone: 18900010002,
address: '西湖区湖底公园1号'
9 years ago
}, {
key: '4',
name: '李夫人',
age: 18,
tel: '0575-22098909',
phone: 18900010002,
address: '西湖区湖底公园1号'
9 years ago
}, {
key: '5',
name: '习大大',
age: 18,
tel: '0575-22098909',
phone: 18900010002,
address: '西湖区湖底公园1号'
}];
ReactDOM.render(<Table columns={columns} dataSource={data} bordered={true} />
, document.getElementById('components-table-demo-colspan-rowspan'));
````