vue3中el-table實(shí)現(xiàn)多表頭并表格合并行或列代碼示例
1、el-table中添加事件 :span-method="genderSpanCity"
<el-table :span-method="genderSpanCity"
:data="data.tableData"
:fit="true" table-layout="fixed" header-align="center" stripe
style="width:100%;height: 96%;"
:cell-style="{borderColor:'#aaa'}"
:header-cell-style="{color:'#000',textAlign:'center',borderColor:' #CCC',background:'#f9f9f9',height:'50px'}"
v-else>
<el-table-column :label="$t('wms.dailyProduct')" align="center" height="70px">
<el-table-column>
<el-table-column prop="process" :label="$t('mes.workingProcedure')" width="100" align="center"/>
<el-table-column prop="item" width="130"/>
<el-table-column prop="item2" width="150"/>
</el-table-column>
<!--二級(jí)標(biāo)題日期-->
<el-table-column v-for="(name,index) in data.title" :key="name" :label="name" align="center">
<!-- 三級(jí)標(biāo)題-->
<el-table-column v-for="column in data.tableColumns" :key="column.prop"
:prop="column.prop"
:label="column.label" align="center">
<template #default="scope">
{{ scope.row.custom.length > 0 ? scope.row.custom[index][column.prop] : scope.row.custom[column.prop] }}
</template>
</el-table-column>
</el-table-column>
</el-table-column>
</el-table>2、js添加函數(shù)
// 合并列
const genderSpanCity = ({
row,
column,
rowIndex,
columnIndex
}) => {
// 合并前4行的2列與3列
for (let i = 0; i < 13; i++) {
if (columnIndex === 1 && rowIndex === i) {
return {
rowspan: 1,
colspan: 2
}
} else if (columnIndex === 2 && rowIndex === i) {
return {
rowspan: 0,
colspan: 0
}
}
}
// 合并第4行以后的數(shù)據(jù)
for (let i = 4; i < data.tableData.length; i++) {
if (columnIndex > 3 && columnIndex % 2 === 0 && rowIndex === i) {
return [1, 3]
} else if (columnIndex >= 3 && columnIndex % 2 === 1 && rowIndex === i) {
return [0, 0]
}
}
// 合并前兩列的數(shù)據(jù)
if (columnIndex === 0 || columnIndex === 1) {
// 獲取當(dāng)前單元格的值
const currentValue = row[column.property]
// 獲取上一行相同列的值
const preRow = data.tableData[rowIndex - 1]
const preValue = preRow ? preRow[column.property] : null
// 如果當(dāng)前值和上一行的值相同,則將當(dāng)前單元格隱藏
if (currentValue === preValue) {
return {
rowspan: 0,
colspan: 0
}
} else {
// 否則計(jì)算當(dāng)前單元格應(yīng)該跨越多少行
let rowspan = 1
for (let i = rowIndex + 1; i < data.tableData.length; i++) {
const nextRow = data.tableData[i]
const nextValue = nextRow[column.property]
if (nextValue === currentValue) {
rowspan++
} else {
break
}
}
return {
rowspan,
colspan: 1
}
}
}
}效果圖為

總結(jié)
到此這篇關(guān)于vue3中el-table實(shí)現(xiàn)多表頭并表格合并行或列的文章就介紹到這了,更多相關(guān)vue3 el-table多表頭表格合并行列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中解決拖拽改變存在iframe的div大小時(shí)卡頓問題
這篇文章主要介紹了vue中解決拖拽改變存在iframe的div大小時(shí)卡頓問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue基于websocket實(shí)現(xiàn)智能聊天及吸附動(dòng)畫效果
這篇文章主要介紹了vue基于websocket實(shí)現(xiàn)智能聊天及吸附動(dòng)畫效果,主要功能是基于websocket實(shí)現(xiàn)聊天功能,封裝了一個(gè)socket.js文件,使用Jwchat插件實(shí)現(xiàn)類似QQ、微信電腦端的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問題
這篇文章主要介紹了Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
elementui實(shí)現(xiàn)預(yù)覽圖片組件二次封裝
這篇文章主要介紹了elementui實(shí)現(xiàn)預(yù)覽圖片組件二次封裝的方法 ,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
使用Vue如何寫一個(gè)雙向數(shù)據(jù)綁定(面試常見)
這篇文章主要介紹了使用Vue如何寫一個(gè)雙向數(shù)據(jù)綁定,在前端面試過程中經(jīng)常會(huì)問到,文中主要實(shí)現(xiàn)v-model,v-bind 和v-click三個(gè)命令,其他命令也可以自行補(bǔ)充。需要的朋友可以參考下2018-04-04
實(shí)例詳解vue.js淺度監(jiān)聽和深度監(jiān)聽及watch用法
這篇文章主要介紹了vue.js淺度監(jiān)聽和深度監(jiān)聽及watch用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
vue實(shí)現(xiàn)富文本編輯器詳細(xì)過程
Vue富文本的實(shí)現(xiàn)可以使用一些現(xiàn)成的第三方庫,如Quill、Vue-quill-editor、wangEditor等,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)富文本編輯器的相關(guān)資料,需要的朋友可以參考下2024-01-01

