2015-10-29 15:38:59 +08:00
|
|
|
# 表格行/列合并
|
|
|
|
|
|
|
|
- order: 13
|
|
|
|
|
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
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
````jsx
|
|
|
|
import { Table } from 'antd';
|
|
|
|
|
2015-10-29 20:15:44 +08:00
|
|
|
// 事例表中第四行合并了五列,除了第一列设置 colSpan = 5 外
|
|
|
|
// 其他列的第四行 colSpan = 0 (被合并掉,不会渲染)
|
2015-10-29 16:23:09 +08:00
|
|
|
const renderContent = function(value, row, index) {
|
2015-10-29 15:38:59 +08:00
|
|
|
let obj = {
|
|
|
|
children: value,
|
2015-10-29 16:23:09 +08:00
|
|
|
props: {}
|
2015-11-25 17:35:49 +08:00
|
|
|
};
|
2015-10-29 16:23:09 +08:00
|
|
|
if (index === 4) {
|
2015-10-29 15:38:59 +08:00
|
|
|
obj.props.colSpan = 0;
|
|
|
|
}
|
|
|
|
return obj;
|
2015-10-29 20:14:08 +08:00
|
|
|
};
|
2015-10-29 15:38:59 +08:00
|
|
|
|
|
|
|
const columns = [{
|
|
|
|
title: '姓名',
|
|
|
|
dataIndex: 'name',
|
|
|
|
render: function(text, row, index) {
|
2015-10-29 16:23:09 +08:00
|
|
|
if (index < 4) {
|
2015-11-25 17:35:49 +08:00
|
|
|
return <a href="#">{text}</a>;
|
2015-10-29 15:38:59 +08:00
|
|
|
}
|
2016-01-07 16:29:12 +08:00
|
|
|
return {
|
|
|
|
children: <a href="#">{text}</a>,
|
|
|
|
props: {
|
|
|
|
colSpan: 5
|
|
|
|
}
|
|
|
|
};
|
2015-10-29 15:38:59 +08:00
|
|
|
}
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-10-29 15:38:59 +08:00
|
|
|
title: '年龄',
|
|
|
|
dataIndex: 'age',
|
|
|
|
render: renderContent
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-10-29 15:38:59 +08:00
|
|
|
title: '家庭电话',
|
|
|
|
colSpan: 2,
|
|
|
|
dataIndex: 'tel',
|
2015-10-29 16:23:09 +08:00
|
|
|
render: function(value, row, index) {
|
2015-10-29 15:38:59 +08:00
|
|
|
let obj = {
|
|
|
|
children: value,
|
2016-01-07 16:29:12 +08:00
|
|
|
props: {}
|
2015-11-25 17:35:49 +08:00
|
|
|
};
|
2015-10-29 16:23:09 +08:00
|
|
|
// 第三列的第三行行合并
|
|
|
|
if (index === 2) {
|
2015-10-29 15:38:59 +08:00
|
|
|
obj.props.rowSpan = 2;
|
|
|
|
}
|
2015-10-29 16:23:09 +08:00
|
|
|
|
2015-10-29 17:31:47 +08:00
|
|
|
// 第三列的第四行被合并没了,设置 rowSpan = 0 直接不用渲染
|
2015-10-29 16:23:09 +08:00
|
|
|
if (index === 3) {
|
2015-10-29 15:38:59 +08:00
|
|
|
obj.props.rowSpan = 0;
|
|
|
|
}
|
2015-10-29 16:23:09 +08:00
|
|
|
|
|
|
|
if (index === 4) {
|
2015-10-29 15:38:59 +08:00
|
|
|
obj.props.colSpan = 0;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-10-29 15:38:59 +08:00
|
|
|
title: '手机号',
|
|
|
|
colSpan: 0,
|
|
|
|
dataIndex: 'phone',
|
|
|
|
render: renderContent
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-10-29 15:38:59 +08:00
|
|
|
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号'
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-10-29 15:38:59 +08:00
|
|
|
key: '4',
|
|
|
|
name: '李夫人',
|
|
|
|
age: 18,
|
|
|
|
tel: '0575-22098909',
|
|
|
|
phone: 18900010002,
|
|
|
|
address: '西湖区湖底公园1号'
|
2015-10-29 16:23:09 +08:00
|
|
|
}, {
|
2015-11-25 17:35:49 +08:00
|
|
|
key: '5',
|
|
|
|
name: '习大大',
|
|
|
|
age: 18,
|
|
|
|
tel: '0575-22098909',
|
|
|
|
phone: 18900010002,
|
|
|
|
address: '西湖区湖底公园1号'
|
|
|
|
}];
|
2015-10-29 15:38:59 +08:00
|
|
|
|
2015-11-25 17:47:55 +08:00
|
|
|
ReactDOM.render(<Table columns={columns} dataSource={data} bordered />
|
2015-12-29 12:08:58 +08:00
|
|
|
, mountNode);
|
2015-10-29 15:38:59 +08:00
|
|
|
````
|