Element-Plus表格實(shí)現(xiàn)Table自定義合并行數(shù)據(jù)
在開發(fā)項(xiàng)目中,我們時(shí)常會(huì)用到表格,許多需求可能會(huì)要求自定義特定的行或列。
接下來,我們將探討在實(shí)際開發(fā)中如何應(yīng)對這一挑戰(zhàn)。
本文案例采用的技術(shù):
| 名稱 | 版本 |
|---|---|
| Vue3 | ^3.5.12 |
| element-plus | ^2.8.8 |
知識(shí)點(diǎn)
我們先來復(fù)習(xí)下2個(gè)知識(shí)點(diǎn),來自element-plus文檔 table:
1、自定義表頭
通過設(shè)置 slot 來自定義表頭。(只貼出重點(diǎn)代碼)
<el-table :data="filterTableData" style="width: 100%">
<el-table-column label="Date" prop="date" />
<el-table-column label="Name" prop="name" />
<el-table-column align="right">
<template #header>
<el-input v-model="search" size="small" placeholder="Type to search" />
</template>
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.$index, scope.row)">
Edit
</el-button>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.$index, scope.row)"
>
Delete
</el-button>
</template>
</el-table-column>
</el-table>
其中可以看出,通過 <template #header> 自定義表頭,<template #default="scope"> 自定義內(nèi)容。
2、合并行或列
多行或多列共用一個(gè)數(shù)據(jù)時(shí),可以合并行或列。
通過給 table 傳入span-method方法可以實(shí)現(xiàn)合并行或列, 方法的參數(shù)是一個(gè)對象,里面包含當(dāng)前行 row、當(dāng)前列 column、當(dāng)前行號 rowIndex、當(dāng)前列號 columnIndex 四個(gè)屬性。 該函數(shù)可以返回一個(gè)包含兩個(gè)元素的數(shù)組,第一個(gè)元素代表 rowspan,第二個(gè)元素代表 colspan。 也可以返回一個(gè)鍵名為 rowspan 和 colspan 的對象。
<el-table
:data="tableData"
:span-method="arraySpanMethod"
border
style="width: 100%"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" sortable label="Amount 1" />
<el-table-column prop="amount2" sortable label="Amount 2" />
<el-table-column prop="amount3" sortable label="Amount 3" />
</el-table>
<script lang="ts" setup>
// 省略其他代碼...
const arraySpanMethod = ({
row,
column,
rowIndex,
columnIndex,
}: SpanMethodProps) => {
if (rowIndex % 2 === 0) {
if (columnIndex === 0) {
return [1, 2]
} else if (columnIndex === 1) {
return [0, 0]
}
}
}
</script>
實(shí)戰(zhàn)開發(fā)
假設(shè)一個(gè)需求:在最后一行新增一條自定義的行數(shù)據(jù)。
結(jié)合上述2個(gè)知識(shí)點(diǎn),我們可以進(jìn)行改進(jìn):
<template>
<el-table :data="tableData" :span-method="arraySpanMethod" border style="width: 500px">
<el-table-column prop="id" label="ID" width="100">
<template #default="scope">
<span v-if="scope.$index === tableData.length - 1">
<span>是否確認(rèn)信息:</span>
<el-radio-group v-model="scope.row.confirmFlag">
<el-radio :value="true">確認(rèn)</el-radio>
<el-radio :value="false">未確認(rèn)</el-radio>
</el-radio-group>
</span>
<template v-else>{{ scope.row.id }}</template>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" />
<el-table-column prop="age" label="年齡" />
</el-table>
</template>
<script setup lang="ts">
import type { TableColumnCtx } from 'element-plus'
interface User {
id?: string
name?: string
age?: number
confirmFlag?: boolean
}
interface SpanMethodProps {
row: User
column: TableColumnCtx<User>
rowIndex: number
columnIndex: number
}
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }: SpanMethodProps) => {
// 最后一行
if (rowIndex === tableData.length - 1) {
// [1,3] 占一行三列
return [1, 3]
}
}
const tableData: User[] = [
{
id: '1',
name: 'Tom1',
age: 20,
},
{
id: '2',
name: 'Tom2',
age: 30,
},
{
id: '3',
name: 'Tom3',
age: 40,
},
// 新增一條自定義的數(shù)據(jù)
{
confirmFlag: true,
},
]
</script>
<style scoped></style>

到此這篇關(guān)于Element-Plus表格實(shí)現(xiàn)Table自定義合并行數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Element-Plus自定義合并行數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制文本內(nèi)容的例子
今天小編就為大家分享一篇Vue實(shí)現(xiàn)點(diǎn)擊按鈕復(fù)制文本內(nèi)容的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
詳解vue實(shí)現(xiàn)坐標(biāo)拾取器功能示例
這篇文章主要介紹了詳解vue實(shí)現(xiàn)坐標(biāo)拾取器功能示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Vue3+Springboot實(shí)現(xiàn)前后端防抖增強(qiáng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue3如何結(jié)合Springboot實(shí)現(xiàn)前后端防抖增強(qiáng)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
VUE學(xué)習(xí)寶典之el-dialog使用示例
在我工作過程中使用el-dialog的需求挺多的,也積累了一下使用技巧,這篇文章主要給大家介紹了關(guān)于VUE學(xué)習(xí)寶典之el-dialog使用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
vue組件之間通信方式實(shí)例總結(jié)【8種方式】
這篇文章主要介紹了vue組件之間通信方式,結(jié)合實(shí)例形式總結(jié)分析了vue.js的8種組件通信方式與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-02-02
詳解如何實(shí)現(xiàn)Vue組件的動(dòng)態(tài)綁定
Vue.js 是一個(gè)漸進(jìn)式框架,用于構(gòu)建用戶界面,在開發(fā)過程中,我們經(jīng)常需要根據(jù)不同的條件動(dòng)態(tài)顯示組件,在本文中,我將詳細(xì)介紹如何實(shí)現(xiàn)Vue組件的動(dòng)態(tài)綁定,提供示例代碼,以幫助你更深入地理解這個(gè)概念,需要的朋友可以參考下2024-11-11

