//页面table 加入方法
<el-table :span-method="objectSpanMethod">
// 将获取的数据进行合并
this.getMergeObj(this.tableData);
/**
* 获取合并对象
* @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)
},
// 单元格合并 纵向及横向合并 lable = ‘targetName / targetChildren’
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
let col = 1;
if(row.targetName == row.targetChildren && column.property === 'targetName'){
col = 2;
}
if (row.targetName == row.targetChildren && column.property === 'targetChildren'){
col = 0;
}
// 需要合并的行
if (column.property === 'targetName' || column.property === 'targetChildren') {
return {
rowspan: this.mergeObj[column.property][rowIndex],
colspan: col
}
} else {
return { rowspan: 1, colspan: 1 }
}
//row:行对象,带有一行所有数据,column:列对象
//rowIndex:行索引,columnIndex:列索引
//加载表格时从(0,0)开始,(0,1)···(1,0),(1,1)···依次获取数据
if(columnIndex===2){
//每次走到第3列时给单元格加上rowspan和colspan属性
const _row = this.tableData[rowIndex];//从处理完的数组里获取
const _col = _row>0?1:0;
return {
rowspan:_row,
colspan:_col //相当于给给表格加上rowspan,colspan属性
}
}
},
单元格横向、纵向合并Vue
于 2022-08-11 14:05:02 首次发布