objectSpanMethod({ row, column, rowIndex, columnIndex }) {
//定义需要合并的列字段,有哪些列需要合并,就自定义添加字段即可
const fields = ['deptAdded']
// 当前行的数据
const cellValue = row[column.property]
// 判断只合并定义字段的列数据
if (cellValue && fields.includes(column.property)) {
const prevRow = this.lists[rowIndex - 1] //上一行数据
let nextRow = this.lists[rowIndex + 1] //下一行数据
// 当上一行的数据等于当前行数据时,当前行单元格隐藏
if (prevRow && prevRow[column.property] === cellValue) {
return { rowspan: 0, colspan: 0 }
} else {
// 反之,则循环判断若下一行数据等于当前行数据,则当前行开始进行合并单元格
let countRowspan = 1 //用于合并计数多少单元格
while (nextRow && nextRow[column.property] === cellValue) {
nextRow = this.lists[++countRowspan + rowIndex]
}
if (countRowspan > 1) {
return { rowspan: countRowspan, colspan: 1 }
}
}
}
}
调用
<el-table
:data="lists"
max-height="620"
stripe
border
:default-sort="{ prop: 'id', order: 'ascending' }"
:header-cell-style="tableHeaderColor"
:row-key="getRowKey"
:span-method="objectSpanMethod"
></el-table