data里先定义spanArr:[],pos:0,dataList是表格数据
data() {
return {
dataList:[],
spanArr:[],
pos:0
}
}
this.dataList = []
this.spanArr = []
this.pos = 0
for (var i = 0; i < this.dataList.length; i++) {
if (i === 0) {
this.spanArr.push(1);
this.pos = 0;
} else {
// 判断当前元素与上一个元素是否相同 unit单位名称
if (this.dataList[i].unit === this.dataList[i - 1].unit) {
this.spanArr[this.pos] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.pos = i;
}
}
}
随后在table里绑定合并单元格方法:span-method="tableSpanMethod"
此例合并的是1,2列满足的行
tableSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0 || columnIndex === 1) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return [_row,_col]
}
}