使用的是element中的 :span-method="objectSpanMethod"
效果图:
数据:
this.listData = [
{
bom: { id: 1 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 1 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 3 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
];
需求是把每一列数据中相同的bom.id合并
源码奉上:
<template>
<div class="">
<el-table
:data="listData"
:span-method="objectSpanMethod"
class="tableArea"
style="width: 100%"
>
<el-table-column
prop="parentBomVersion"
label="序号"
align="center"
width="200"
/>
<el-table-column prop="updateTime" label="工单类型" />
<el-table-column prop="parentMaterialName" label="taskKey" />
<el-table-column prop="parentMaterialSpecification" label="templateUrl" />
<el-table-column label="操作">
<template slot-scope="scope">
<el-tooltip
class="item"
effect="dark"
content="修改"
placement="top-start"
>
<i class="el-icon-edit-outline" @click="modify(scope)" />
</el-tooltip>
<el-tooltip
class="item"
effect="dark"
content="删除"
placement="top-start"
>
<i class="el-icon-delete" @click="deleteData(scope)" />
</el-tooltip>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: "myNeedDeal",
data() {
return {
rowList: [],
spanArr: [],
position: 0,
listData: [],
};
},
methods: {
queryData() {
//查询操作
this.listData = [
{
bom: { id: 1 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 1 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 2 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
{
bom: { id: 3 },
parentBomVersion: 1,
updateTime: "事件单",
parentMaterialName: "shijian_01",
parentMaterialSpecification: "/shijian_01",
},
];
this.rowspan();
},
rowspan() {
this.rowList = [];
this.spanArr = [];
this.position = 0;
this.listData.forEach((item, index) => {
if (index === 0) {
this.spanArr.push(1);
this.position = 0;
} else {
if (this.listData[index].bom.id === this.listData[index - 1].bom.id) {
this.spanArr[this.position] += 1;
this.spanArr.push(0);
console.log(this.spanArr, this.position);
} else {
this.spanArr.push(1);
this.position = index;
}
}
});
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
//表格合并行
if (columnIndex === 0) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col,
};
}
if (columnIndex === 1) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col,
};
}
},
},
mounted() {
this.queryData();
},
};
</script>
其中 rowspan() 方法是筛选出需要合并的单元格。