el-table 表格, 動態(tài)合并相同內(nèi)容, 支持分頁
核心代碼
### html 代碼
el-table(
:span-method="arraySpanMethod"
:data="activities"
max-height="540px"
border)
el-table-column(
v-for="(item, index) in columns"
:key="item.prop"
v-bind="item")
### JS 代碼
methods: {
/**
* 合并單元格
* @param {Object} args[0].row 當前行
* @param {Object} args[0].column 當前列
* @param {Object} args[0].rowIndex 當前行索引
* @param {Object} args[0].columnIndex 當前列索引
* @return {Object}
*/
arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
// 需要合并的列
if (column.property === 'firName' || column.property === 'secName') {
return {
rowspan: this.mergeObj[column.property][rowIndex],
colspan: 1
}
} else {
return { rowspan: 1, colspan: 1 }
}
},
/**
* 獲取合并對象
* @param {Object} list 列表
*/
getMergeObj (list) {
this.mergeObj = {}
// 標記
let mack = 0
for (let key in list[0]) {
this.mergeObj[key] = []
mack = 0
list.forEach((item, index) => {
if (index === 0) {
this.mergeObj[key].push(1)
} else {
if (item[key] === list[index - 1][key] && item[key]) {
this.mergeObj[key][mack] += 1
this.mergeObj[key].push(0)
} else {
mack = index
this.mergeObj[key].push(1)
}
}
})
}
console.log('-合并對象:', this.mergeObj)
}
}