el-table中表格合并行是规律合并,如果遇到如下表格该怎么合并呢
一个产品对应多个客户编码,一个客户编码又对应3个分类,那么组件中自带的就不符合需求,所以我们要处理处理
如下:
// 动态合并行处理方法
rowSpanMethod() {
this.spanArr = [];
this.pos = 0;
for (var i = 0; i < this.tableData.length; i++) {
if (i === 0) {
this.spanArr.push(1);
this.pos = 0;
} else {
// 判断当前元素与上一个元素是否相同
// 相同 记录当前位置其值 +1 表示当前行需要合并,并push 一个 0 作为占位表示当前行遇上一行合并了
if (
this.tableData[i].productItemNo ===
this.tableData[i - 1].productItemNo
) {
this.spanArr[this.pos] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.pos = i; //记录位置
}
}
}
},
// 合并行
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
// 合并产品
if (columnIndex === 0) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return [_row, _col];
}
// 合并客户编码
if (["customerNo"].includes(column.property)) {
if (rowIndex % 3 == 0) {
return {
rowspan: 3,
colspan: 1,
};
} else {
return {
rowspan: 0,
colspan: 0,
};
}
}
}
},
完成!
欢迎各位大佬指教~