vue.js el-table動(dòng)態(tài)單元格列合并方式
一、業(yè)務(wù)需求
一個(gè)展示列表,表格中有一部分列是根據(jù)后端接口動(dòng)態(tài)展示,對(duì)于不同類型的數(shù)據(jù)展示效果不一樣。
如果接口返回?cái)?shù)據(jù)是’類型1‘的,則正常展示,如果是’類型2‘的數(shù)據(jù),則合并當(dāng)前數(shù)據(jù)的動(dòng)態(tài)表格。

二、實(shí)現(xiàn)思路
1、先將普通表格實(shí)現(xiàn),不考慮合并效果;
2、在表格上對(duì)要合并的單元格類型進(jìn)行區(qū)分;
3、 在表格上使用:span-method="arraySpanMethod"方法觸發(fā)表格;
4、在arraySpanMethod方法內(nèi)接收數(shù)據(jù)處理合并,確定從哪一列開(kāi)始合并到哪一列合并結(jié)束;
三、代碼展示
<el-table
ref="table"
size="mini"
height="100%"
:data="tableData"
:span-method="arraySpanMethod"
:header-cell-style="{
background: '#f5f7fa',
fontWeight: 'bold',
color: '#303133'
}"
border
>
<el-table-column
type="index"
header-align="center"
align="center"
label="序號(hào)"
width="50"
></el-table-column>
<el-table-column
width="120"
prop="indexShowName"
label="名稱"
show-overflow-tooltip
></el-table-column>
<el-table-column
width="80"
prop="type"
label="類型種類"
show-overflow-tooltip
>
<template slot-scope="scope">
{{ scope.row.type === '1' ? '類型1' : '類型2' }}
</template>
</el-table-column>
<el-table-column
v-for="(item, index) in tableColumns"
:key="index"
width="80"
:label="item.year"
show-overflow-tooltip
>
<template slot-scope="scope">
<!-- 類型1展示name -->
<div
v-if="scope.row.type === '1'"
style="text-align: center"
>
{{
scope.row.uploadValueList[index]?.uploadValueName
}}
</div>
<!-- 類型2展示value -->
<div v-else>
{{ scope.row.uploadValueList[index].uploadValue }}
</div>
</template>
</el-table-column>
<el-table-column
width="160"
prop="reportDate"
label="上報(bào)時(shí)間"
show-overflow-tooltip
></el-table-column>
<el-table-column
width="195"
label="操作"
header-align="center"
align="center"
fixed="right"
>
<template slot-scope="scope">
<el-button
size="small"
style="color: #409eff; padding: 0"
type="text"
@click="detailClick(scope.row)"
>數(shù)據(jù)明細(xì)</el-button
>
</template>
</el-table-column>
</el-table>
// --------------methods方法--------------------
// 右側(cè)表格
initTable() {
const params = {
pageNum: this.pages.pageIndex,
pageSize: this.pages.pageSize,
}
this.tableLoading = true
//api接口調(diào)用xxx
xxx(params)
.then((res) => {
if (res.code === 200) {
const { total } = res.result
// const { records, total } = res.result
//后端接口數(shù)據(jù)返回形式如下:
const records = [
{
indexShowName: '測(cè)試001',
type: '1',
reportDate: '2023-12-01 15:53:46',
uploadValueList: [
{
id: '1',
year: '2021年',
uploadValue: '0',
uploadValueName: '完全符合'
},
{
id: '2',
year: '2022年',
uploadValue: '0',
uploadValueName: '完全符合'
},
{
id: '3',
year: '2023年',
uploadValue: '0',
uploadValueName: '完全符合'
},
{
id: '4',
year: '2024年',
uploadValue: '0',
uploadValueName: '完全符合'
}
]
},
{
indexShowName: '測(cè)試002',
type: '2',
reportDate: '2023-12-01 13:45:53',
uploadValueList: [
{
id: '5',
year: '2021年',
uploadValue: '99.00'
},
{
id: '6',
year: '2022年',
uploadValue: '98.00'
},
{
id: '7',
year: '2023年',
uploadValue: '77.00'
},
{
id: '8',
year: '2024年',
uploadValue: '34.00'
}
]
}
]
if (records && records.length > 0) {
// 使用第一個(gè)元素的 uploadValueList 來(lái)創(chuàng)建列
this.tableColumns = records[0].uploadValueList.map((item) => ({
year: item.year, // 使用 year 作為列的標(biāo)簽
id: item.id // 用于做key
}))
}
this.tableData = records
this.pages.total = total
} else {
this.$message.error(res.message)
}
})
.finally(() => {
this.tableLoading = false
})
},
// 單元格合并 {當(dāng)前行row、當(dāng)前列column、當(dāng)前行號(hào)rowIndex、當(dāng)前列號(hào)columnIndex}
arraySpanMethod({ row, column, rowIndex, columnIndex }) {
// 類型1,且動(dòng)態(tài)數(shù)據(jù)長(zhǎng)度>1
if (row.type === '1' && row?.uploadValueList?.length > 1) {
const len = row?.uploadValueList?.length
// 合并從下標(biāo)為0開(kāi)始的【下標(biāo)為3的第四列,動(dòng)態(tài)數(shù)據(jù)長(zhǎng)度】
if ( columnIndex > 2 && columnIndex <= 2 + Number(len) ) {
return {
rowspan: 1,
colspan: columnIndex === 3 ? len : 0
}
}
}
},
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3中Composition API的原理與實(shí)戰(zhàn)指南
Composition API為開(kāi)發(fā)者提供了一種全新的組織組件邏輯的方式,本文將深入探討Vue3中Composition API的實(shí)際應(yīng)用,幫助開(kāi)發(fā)者掌握這一強(qiáng)大工具2025-07-07
vue2實(shí)現(xiàn)可復(fù)用的輪播圖carousel組件詳解
這篇文章主要為大家詳細(xì)介紹了vue2實(shí)現(xiàn)可復(fù)用的輪播圖carousel組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐
這篇文章主要介紹了基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
vue長(zhǎng)按事件和點(diǎn)擊事件沖突的解決
這篇文章主要介紹了vue長(zhǎng)按事件和點(diǎn)擊事件沖突的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue使用localStorage保存登錄信息 適用于移動(dòng)端、PC端
這篇文章主要為大家詳細(xì)介紹了vue使用localStorage保存登錄信息 適用于移動(dòng)端、PC端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
在vue中使用vue-echarts-v3的實(shí)例代碼
這篇文章主要介紹了在vue中使用vue-echarts-v3的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
vue實(shí)現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實(shí)現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01

