<el-table :span-method="cellMerge" :key="tableKey" v-loading="tyyyLoading" :data="tyyyData" border :header-cell-style="rowClass" :cell-style="cellStyle">
<el-table-column v-for="(item, index) in tyyyTableList" :key="index" align="center" :label="item.label">
<template slot-scope="scope">
<span :title="scope.row[`${item.prop}`]" style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis">{{ scope.row[item.prop] }}</span>
</template>
</el-table-column>
</el-table>
2、写法
// 获取相同名称的个数 tableData: 表格的数据, projectName: 确定相同的参数
cellMerge({ row, column, rowIndex, columnIndex }) {
if ( (rowIndex === 0 || rowIndex == 5) && columnIndex === 0) {
return [1, 2];
} else if ((rowIndex === 0 || rowIndex == 5) && columnIndex === 1) {
return [0,2]; // 再把前面合并了的那一列删了,这样错位的再错回来,往前一列又对了
} else if (columnIndex === 0) {
const _row = this.spanArr[rowIndex]// 处理除了第一行和第五行外的单元格和合并,但是也只是合并第一列的产品类型相同的数据
const _col = 1
return {
rowspan: _row,
colspan: _col,
}
} else {
return [1, 1];//其他单元格都独立显示
}
},
// 单元格的处理方法 当前行row、当前列column、当前行号rowIndex、当前列号columnIndex
getSpanArr(data) {
for (let i = 0; i < data.length; i++) {
if (i === 0) {
this.spanArr = [];
this.spanArr.push(1);
this.pos = 0;
} else {
if (data[i].productTypeDesc === data[i - 1].productTypeDesc) {
this.spanArr[this.pos] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.pos = i;
}
}
}
return this.spanArr; // 返回合并信息数组
},
3 、最终效果