el-table相同名字单元格合并

1,122 阅读1分钟

el-table的合并单元格用span-method方法实现,官方给的例子比较简单,现在要实现合并第一列相同名字的单元格,自己想了会儿觉得写起来怪麻烦,网上找到一个方法(翻不到出处了),在此记录一下,方法不重要,主要看思路。

<el-table :data="tableData" :span-method="objectSpanMethod" ></el-table>

getSpanArr(data) {

this.spanArr = []

for (var i = 0; i < data.length; i++) {

if (i === 0) {

this.spanArr.push(1) this.pos = 0

} else {

// 判断当前元素与上一个元素是否相同 inAccessCode(批次字段)

if (data[i].houseName === data[i - 1].houseName) {

this.spanArr[this.pos] += 1 this.spanArr.push(0)

} else { this.spanArr.push(1) this.pos = i } } } console.log(this.spanArr) },

//合并单元格

objectSpanMethod({ row, column, rowIndex, columnIndex }) {

if (columnIndex === 0) {

const _row = this.spanArr[rowIndex]

const _col = _row > 0 ? 1 : 0

return { rowspan: _row, colspan: _col, }

}

}