效果如下图所示,只合并相邻相同行
列表数据: 0不显示 1独占一行 其它如3合并3行
export const columns: BasicColumn[] = [
{
title: '用途层级1',
align: 'center',
width: 100,
dataIndex: 'useLevel1',
customCell: (record, index, column) => {
return { rowSpan: record.useLevel1Rowspan ?? 1 };
},
},
{
title: '用途层级2',
align: 'center',
width: 100,
dataIndex: 'useLevel2',
customCell: (record, index, column) => {
return { rowSpan: record.useLevel2Rowspan ?? 1 };
},
},
{
title: '用途层级3',
align: 'center',
width: 100,
dataIndex: 'useLevel3',
customCell: (record, index, column) => {
return { rowSpan: record.useLevel3Rowspan ?? 1 };
},
},
{
title: '用途层级4',
align: 'center',
width: 100,
dataIndex: 'useLevel4',
customCell: (record, index, column) => {
return { rowSpan: record.useLevel4Rowspan ?? 1 };
},
},
{
title: '能源类别',
align: 'center',
width: 100,
dataIndex: 'energyType',
}
]
组件内使用
import { columns } from './jsfile.data';
let newTableData = reactive([]);
let isMerge = ref(false);
const needTomergeColumnsIndex = ['useLevel1', 'useLevel2', 'useLevel3', 'useLevel4'];
watch(tableDataSource, async (newTableDataSource) => {
if (newTableDataSource && newTableDataSource.length > 1 && !isMerge.value) {
let tableData = newTableDataSource;
isMerge.value = true;
await needTomergeColumnsIndex.forEach((columnDataIndex) => {
const dynamicVariables = {};
const variableName = columnDataIndex + 'Rowspan';
tableData.forEach((item, index) => {
dynamicVariables[variableName] = 0;
if (tableData[index - 1] && item[columnDataIndex] === tableData[index - 1][columnDataIndex]) {
dynamicVariables[variableName] = 0;
} else {
for (let i = index; i < tableData.length; i++) {
if (tableData[i][columnDataIndex] === item[columnDataIndex]) {
dynamicVariables[variableName]++;
} else {
break;
}
}
}
item[variableName] = dynamicVariables[variableName];
});
console.log(tableData);
});
newTableData = tableData;
}
});