Element vue :span-method=“objectSpanMethod“ 实现跨列-----合并
1. 下面图是实现效果2. 代码实现1、html页面<el-table :data="tableData"bordersize="mini"align="center":span-method="objectSpanMethod"><el-table-column prop="deviceLocation"...
·
1. 下面图是实现效果
2. 代码实现
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>
data中定义position: 0,rowSpanArr: [],
2、js代码
// 获取合并的数组
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();!!!
如有不会的内容. 请在下面留言
更多推荐
已为社区贡献4条内容
所有评论(0)