el-table span-method 与空白单元格合并
原始excel表格样式:
页面呈现的表格样式:
代码如下
// An highlighted block
<el-table :data="showList" :span-method="arraySpanMethod">
...
</el-table>
// 数据处理
dealData (rowCell) {
let indexList= [], locationList = []
// 如果单元格不为空,找到对应的index
list[1].forEach((item, index) => {if (item[rowCell] != '') {indexList.push(index)}});
// 数据最后一项也push进去,用于合并总列数计算
indexList.push(list[1].length)
// 计算有数据列距下一个有数据列的列数:rowspan
indexList.forEach((item, index) => {
if (index == indexList.length - 1) return
locationList[`'${item}'`] = ({ rowspan: indexList[index + 1] - item, colspan: 1 })
})
return locationList
},
// 单元格合并方法
arraySpanMethod ({ row, rowIndex, columnIndex }) {
if (columnIndex === 1) { // 此处代表我要合并第一列
if (row.t1 != '') { // 如果此单元格不为空,进行数据处理
// 处理合并数据
let locationList = this.dealData('t1')
return locationList[`'${rowIndex}'`]
}
else { // 为空则隐藏
return {
rowspan: 0,
colspan: 0
};
}
}
欢迎指正,over 😃