数据格式为:
tableData6: [
{
id: "1",
name: "王小虎",
amount1: "234"
},
{
id: "1",
name: "王小虎",
amount1: "165"
},
{
id: "2",
name: "王小虎",
amount1: "324"
},
{
id: "2",
name: "王小虎",
amount1: "621"
},
{
id: "2",
name: "王小虎",
amount1: "539"
}
]
}
实现代码:
methods: {
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) { // 假设name在第一列
if (rowIndex > 0) {
const prevRow = this.tableData6[rowIndex - 1];
if (row.name === prevRow.name) {
return {
rowspan: 0,
colspan: 0
};
}
}
let sameNameCount = 1;
for (let i = rowIndex + 1; i < this.tableData6.length; i++) {
if (this.tableData6[i].name === row.name) {
sameNameCount++;
} else {
break;
}
}
return {
rowspan: sameNameCount,
colspan: 1
};
}
}
}
<template>
<el-table
:data="tableData6"
style="width: 100%"
:span-method="objectSpanMethod"
>