一、需求描述,如图,相同的区域位置进行合并,前提:后台给的数据相同区域名称的要放在临近。
二、代码实现
1、html页面
<el-table :data="tableData"
border
size="mini"
align="center"
:span-method="objectSpanMethod">
<el-table-column prop="deviceLocation"
label="区域名称"
align="center"></el-table-column>
<el-table-column prop="deviceName"
label="设备名称"
align="center"></el-table-column
<el-table-column prop="hour23"
label="23"
width="40"
align="center">
<template slot-scope="scope">
<span class="my-span"
v-if="scope.row.hour23 == 1">√</span>
<span class="my-span"
v-if="scope.row.hour23 == 2">○</span>
<span class="my-span"
v-if="scope.row.hour23 == 3">△</span>
<span class="my-span"
v-if="scope.row.hour23 == 4">×</span>
<span v-if="scope.row.hour23 == -1">—</span>
</template>
</el-table-column>
</el-table>
2、js代码
data中定义position: 0,rowSpanArr: [],
// 获取合并的数组
getRowSpan() {
this.rowSpanArr = [];
this.tableData.forEach((item, index) => {
if (index == 0) {
this.rowSpanArr.push(1);
this.position = 0;
} else {
if (this.tableData[index].deviceLocation == this.tableData[index - 1].deviceLocation) {
this.rowSpanArr[this.position] += 1; //项目名称相同,合并到同一个数组中
this.rowSpanArr.push(0);
this.tableData[index].deviceLocation = this.tableData[index - 1].deviceLocation;
} else {
this.rowSpanArr.push(1);
this.position = index;
}
}
});
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
// 只合并区域位置
if (columnIndex === 0) {
const _row = this.rowSpanArr[rowIndex];
return {
rowspan: _row, //行
colspan: 1 //列
};
}
}
注意:列表数据获取完毕之后,调用this.getRowSpan();!!!