element ui合并表格
这是官网上的效果,传送门
这是我合并之后 的样子,
首先要保证你的数据是一条一条的,然后要合并的地方得有一个地方是用于区分到底合并不合并的,
上代码
<el-table
:data="listData"
:span-method="objectSpanMethod"
class="tableArea"
style="width: 100%"
>
<el-table-column prop="type" label="品种" />
<el-table-column prop="child" label="內容"/>
<el-table-column prop="num" label="数量" />
<el-table-column prop="total" label="总数量" />
</el-table>
data() {
return {
arr: [],
rowList: [],
spanArr: [],
position: 0,
listData: [],
};
},
created() {
//因为后端返给我的结构是嵌套格式的所以先要整理一下数组
this.arr.forEach((element) => {
element.options.forEach((item) => {
let obj = {};
obj = item;
obj.type= element.type;
obj.code = element.code;//用于标识相同行的唯一值
this.listData.push(obj);
});
});
console.log(this.listData);
this.rowspan();
},
methods: {
rowspan() {
this.listData.forEach((item, index) => {
if (index === 0) {
this.spanArr.push(1);
this.position = 0;
} else {
if (
this.listData[index].code ===
this.listData[index - 1].code
) {
this.spanArr[this.position] += 1;
this.spanArr.push(0);
} 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,
};
}
},
},