样式
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/168e739a111d4f54872bcafe3084029c.png)
代码
<template>
<div>
<el-table
:data="tableData"
:span-method="objectSpanMethod"
border
style="width: 100%; margin-top: 20px"
>
<el-table-column prop="id" label="类型" width="180"> </el-table-column>
<el-table-column prop="name" label="姓名"> </el-table-column>
<el-table-column prop="amount3" label="数值 3(元)"> </el-table-column>
<el-table-column label="合计">
<template slot-scope="scope">
{{ getSum(scope.row.id) }}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
props: {},
data() {
return {
tableData: [
{
id: "A320",
name: "343",
amount3: 10,
},
{
id: "A320",
name: "222",
amount3: 12,
},
{
id: "A320",
name: "23g",
amount3: 9,
},
{
id: "A330",
name: "445",
amount3: 17,
},
{
id: "A330",
name: "54M",
amount3: 15,
},
{
id: "B737",
name: "44H",
amount3: 15,
},
{
id: "B737",
name: "44HG",
amount3: 15,
},
],
sums: {},
};
},
computed: {},
methods: {
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
console.log(row, column, "column");
if (columnIndex === 0 || columnIndex === 3) {
const prevRow = this.tableData[rowIndex - 1];
if (prevRow && row.id === prevRow.id) {
return {
rowspan: 0,
colspan: 1,
};
} else {
let countRowspan = 1;
for (let i = rowIndex + 1; i < this.tableData.length; i++) {
if (this.tableData[i].id === row.id) {
countRowspan++;
} else {
break;
}
}
return {
rowspan: countRowspan,
colspan: 1,
};
}
}
},
getSum(id) {
if (!this.sums[id]) {
this.sums[id] = this.tableData.reduce(
(total, current) =>
current.id === id ? total + current.amount3 : total,
0
);
}
return this.sums[id];
},
},
};
</script>
<style scoped lang="scss">
/deep/ .el-table__row > td {
border: 1px solid #f0f0f0 !important;
}
</style>