Element-Plus表格實(shí)現(xiàn)Table自定義合并行數(shù)據(jù)
在開發(fā)項目中,我們時常會用到表格,許多需求可能會要求自定義特定的行或列。
接下來,我們將探討在實(shí)際開發(fā)中如何應(yīng)對這一挑戰(zhàn)。
本文案例采用的技術(shù):
| 名稱 | 版本 |
|---|---|
| Vue3 | ^3.5.12 |
| element-plus | ^2.8.8 |
知識點(diǎn)
我們先來復(fù)習(xí)下2個知識點(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、合并行或列
多行或多列共用一個數(shù)據(jù)時,可以合并行或列。
通過給 table 傳入span-method方法可以實(shí)現(xiàn)合并行或列, 方法的參數(shù)是一個對象,里面包含當(dāng)前行 row、當(dāng)前列 column、當(dāng)前行號 rowIndex、當(dāng)前列號 columnIndex 四個屬性。 該函數(shù)可以返回一個包含兩個元素的數(shù)組,第一個元素代表 rowspan,第二個元素代表 colspan。 也可以返回一個鍵名為 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è)一個需求:在最后一行新增一條自定義的行數(shù)據(jù)。
結(jié)合上述2個知識點(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)文章
vue3如何解決各場景l(fā)oading過度(避免白屏尷尬!)
在開發(fā)的過程中點(diǎn)擊提交按鈕,或者是一些其它場景總會遇到loading加載,下面這篇文章主要給大家介紹了關(guān)于vue3如何解決各場景l(fā)oading過度的相關(guān)資料,避免白屏尷尬,需要的朋友可以參考下2023-03-03
vue?require.context()的用法實(shí)例詳解
require.context是webpack提供的一個api,通常用于批量注冊組件,下面這篇文章主要給大家介紹了關(guān)于vue?require.context()用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue利用相反數(shù)實(shí)現(xiàn)飄窗動畫效果
飄窗,即一個類似小窗子的在網(wǎng)頁上移動的矩形元素,通常被用于一些通知類的應(yīng)用場景。本文將利用相反數(shù)實(shí)現(xiàn)這一動畫效果,需要的可以參考一下2022-05-05
vue emit之Property or method “$$v“ i
這篇文章主要介紹了vue emit之Property or method “$$v“ is not defined的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

