I got an original table like this
| category | name | price | count | subtotal |
|----------|-------|--------|-------|----------|
| fruit | apple | 2 | 10 | 20 |
|----------|-------|--------|-------|----------|
| fruit | apple | 3 | 20 | 60 |
|----------|-------|--------|-------|----------|
| meat | pork | 4 | 10 | 40 |
|----------|-------|--------|-------|----------|
| meat | beef | 5 | 20 | 50 |
and I try a method to merge the cells with same category, name, now it becomes:
| category | name | price | count | subtotal |
|----------|-------|--------|-------|----------|
| | | 2 | 10 | 20 |
| fruit | apple |--------|-------|----------|
| | | 3 | 20 | 60 |
|----------|-------|--------|-------|----------|
| | | 4 | 10 | 40 |
| meat | pork |--------|-------|----------|
| | | 5 | 20 | 50 |
but in the above table the beef cell disappeared, What I expect is:
| category | name | price | count | subtotal |
|----------|-------|--------|-------|----------|
| | | 2 | 10 | 20 |
| fruit | apple |--------|-------|----------|
| | | 3 | 20 | 60 |
|----------|-------|--------|-------|----------|
| | pork | 4 | 10 | 40 |
| meat |-------|--------|-------|----------|
| | beef | 5 | 20 | 50 |
And also I want to calculate the subtotal of the same items, like the following:
| category | name | price | count | subtotal | subtotal2 |
|----------|-------|--------|-------|----------|-----------|
| | | 2 | 10 | 20 | |
| fruit | apple |--------|-------|----------| 80 |
| | | 3 | 20 | 60 | |
|----------|-------|--------|-------|----------|-----------|
| | pork | 4 | 10 | 40 | |
| meat |-------|--------|-------|----------| 90 |
| | beef | 5 | 20 | 50 | |
note:
I can merge the subtotal2 cell by adding columnIndex === 5 to the objectSpanMethod but the number isn't what I want: (now in subtotal2 it only shows the subtotal of the first row of each item, but I want the sum of the subtotals)
| category | name | price | count | subtotal | subtotal2 |
|----------|-------|--------|-------|----------|-----------|
| | | 2 | 10 | 20 | |
| fruit | apple |--------|-------|----------| 20 |
| | | 3 | 20 | 60 | |
|----------|-------|--------|-------|----------|-----------|
| | pork | 4 | 10 | 40 | |
| meat |-------|--------|-------|----------| 40 |
| | beef | 5 | 20 | 50 | |
Here's my code:
HTML
<div id="app">
<template>
</el-table>
<h1>DataTable<h1>
<el-table
:data="dataTable"
:span-method="objectSpanMethod"
style="width: 100%">
<el-table-column
prop="category"
label="category"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="name"
width="180">
</el-table-column>
<el-table-column
prop="price"
label="price"
width="180">
</el-table-column>
<el-table-column
prop="count"
label="count"
width="180">
</el-table-column>
<el-table-column
label="subtotal"
align="center"
prop="subtotal"
width="180">
<template slot-scope="scope"> {{scope.row.subtotal=scope.row.price*scope.row.count}}
</template>
</el-table-column>
</el-table>
</template>
</div>
JS
data() {
return {
dataTable: [{
category: 'fruit',
name: 'apple',
price: 2,
count: 10,
subtotal:1
},{
category: 'fruit',
name: 'apple',
price: 3,
count: 20,
subtotal:1
},{
category: 'meat',
name: 'pork',
price: 4,
count: 10,
subtotal:1
},{
category: 'meat',
name: 'beef',
price: 5,
count: 10,
subtotal:1
}],
spanArr: [],
}
},
methods:{
onMergeLines() {
this.dataTable.forEach((item, index) => {
if (index === 0) {
this.spanArr.push(1);
this.position = 0;
} else {
if (
this.dataTable[index].category ===
this.dataTable[index - 1].category
) {
this.spanArr[this.position] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.position = index;
}
}
});
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
console.log(row, column);
if (columnIndex === 0 || columnIndex === 1 ) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col,
};
}
}
},
mounted() {
this.onMergeLines();
},
}
Also CodePen:https://codepen.io/itayueat/pen/qBPrYdV
Please let me know if you got have any idea to make this table, thank you!