Ant Design Vue实现a-table表格单元格合并
参考Ant design Vue官网关于行和列合并api
https://antdv.com/components/table-cn/
下面官网对表格合并这块的描述
!!!注意注意注意重要事情说三遍:很多同学都习惯性的将columns写在data外,如果使用表格合并,会报错,提示缺少‘h’,定义的columns一定要写在data中,否则在加载过程中由于渲染顺序会导致其中的渲染函数无法识别
<template>
<a-table :columns="columns" :data-source="data" bordered>
<template slot="name" slot-scope="text">
<a>{{ text }}</a>
</template>
</a-table>
</template>
<script>
const renderContent = (value, row, index) => {
const obj = {
children: value, \\ 这是渲染列的值,支持渲染html
attrs: {}, \\ 属性有colSpan和 rowSpan, colSpan 或者 rowSpan 设值为 0 时,设置的表格不会渲染。设置的值为多少代表合并的行列为多少
};
if (index === 4) {
obj.attrs.colSpan = 0; \\ 如果下标是4,改列不渲染
}
return obj;
};
const data = [
{
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',
},
];
export default {
data() {
const columns = [
{
title: 'Name',
dataIndex: 'name',
customRender: (text, row, index) => {
if (index < 4) {
return <a href="javascript:;">{text}</a>;
}
return {
children: <a href="javascript:;">{text}</a>,
attrs: {
colSpan: 5,
},
};
},
},
{
title: 'Age',
dataIndex: 'age',
customRender: renderContent,
},
{
title: 'Home phone',
colSpan: 2,
dataIndex: 'tel',
customRender: (value, row, index) => {
const obj = {
children: value,
attrs: {},
};
if (index === 2) {
obj.attrs.rowSpan = 2;
}
// These two are merged into above cell
if (index === 3) {
obj.attrs.rowSpan = 0;
}
if (index === 4) {
obj.attrs.colSpan = 0;
}
return obj;
},
},
{
title: 'Phone',
colSpan: 0,
dataIndex: 'phone',
customRender: renderContent,
},
{
title: 'Address',
dataIndex: 'address',
customRender: renderContent,
},
];
return {
data,
columns,
};
},
};
</script>
版权声明:本文为qq_42605684原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。