- 给el-table加上:span-method="objectSpanMethod"方法
<el-table :data="tableData"
:span-method="objectSpanMethod">
<el-table-column width="80"
prop="order"
label="序号"
align="center"></el-table-column>
<el-table-column prop="year"
label="年度"
align="center"></el-table-column>
<el-table-column prop="base"
label="递延额度基数"
align="center"></el-table-column>
<el-table-column prop="check"
label="核定递延额度"
align="center"></el-table-column>
<el-table-column prop="actual"
label="实际兑现额度"
align="center"></el-table-column>
</el-table>
- data声明变量
data(){
tableData:[],
spanArr: [],
position: 0
}
- 获取表格数据后调用rowspan方法(拿到表格数据后再调用!)
rowspan () {
this.spanArr = []
this.position = 0
this.tableData.forEach((item, index) => {
if (index === 0) {
this.spanArr.push(1)
this.position = 0
item.sequence = index + 1
} else {
if (this.tableData[index].order === this.tableData[index - 1].order) {
this.spanArr[this.position] += 1
this.spanArr.push(0)
this.tableData[index].sequence = this.tableData[index - 1].sequence
} else {
this.spanArr.push(1)
this.position = index
this.tableData[index].sequence = this.tableData[index - 1].sequence + 1
}
}
})
},
objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 2 || columnIndex === 3) {
if (rowIndex % 4 === 0) {
return {
rowspan: 4,
colspan: 1
}
} else {
return {
rowspan: 0,
colspan: 0
}
}
} else if (columnIndex === 0 || columnIndex === 1) {
const _row = this.spanArr[rowIndex]
const _col = _row > 0 ? 1 : 0
return {
rowspan: _row,
colspan: _col
}
}
}