<template>
<div>
<h2>行合并</h2>
<el-table :data="tableData" :span-method="objectSpanMethod" border style="width: 100%; margin-top: 20px">
<el-table-column prop="id" label="ID" width="180"> </el-table-column>
<el-table-column prop="name" label="姓名"> </el-table-column>
<el-table-column prop="amount1" label="数值 1(元)"> </el-table-column>
<el-table-column prop="amount2" label="数值 2(元)"> </el-table-column>
<el-table-column prop="amount3" label="数值 3(元)"> </el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: 'SpanArrDemos',
data() {
return {
tableData: [
{
id: '111',
name: '王小虎1',
amount1: '234',
amount2: '3.2',
amount3: 10
},
{
id: '111',
name: '王小虎2',
amount1: '165',
amount2: '4.43',
amount3: 12
},
{
id: '111',
name: '王小虎vvv',
amount1: '324',
amount2: '1.9',
amount3: 9
},
{
id: '222',
name: '王小虎vvv',
amount1: '666',
amount2: '666',
amount3: 17
},
{
id: '333',
name: '王小虎5',
amount1: '539',
amount2: '4.1',
amount3: 15
},
{
id: '333',
name: '王小虎6',
amount1: '539',
amount2: '4.1',
amount3: 15
}
]
}
},
mounted() {
this.getSpanArr(this.tableData, 'id')
},
methods: {
getSpanArr(list = [], attr = '') {
this.spanArr = []
let pos = 0
for (let i = 0; i < list.length; i++) {
if (i === 0) {
this.spanArr.push(1)
} else {
let previous = list[i - 1][attr]
let current = list[i][attr]
if (previous === current) {
this.spanArr[pos] += 1
this.spanArr.push(0)
} else {
pos = i
this.spanArr.push(1)
}
}
}
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
console.log('this.spanArr', this.spanArr)
const cRow = this.spanArr[rowIndex]
const cCol = cRow > 0 ? 1 : 0
let result = {
rowspan: cRow,
colspan: cCol
}
console.log('rowIndex, columnIndex, result', rowIndex + 1, columnIndex + 1, result)
return result
}
}
}
}
</script>