Element-Plus表格實(shí)現(xiàn)Table自定義合并行數(shù)據(jù)
在開發(fā)項(xiàng)目中,我們時(shí)常會(huì)用到表格,許多需求可能會(huì)要求自定義特定的行或列。
接下來,我們將探討在實(shí)際開發(fā)中如何應(yīng)對(duì)這一挑戰(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è)對(duì)象,里面包含當(dāng)前行 row、當(dāng)前列 column、當(dāng)前行號(hào) rowIndex、當(dāng)前列號(hào) columnIndex 四個(gè)屬性。 該函數(shù)可以返回一個(gè)包含兩個(gè)元素的數(shù)組,第一個(gè)元素代表 rowspan,第二個(gè)元素代表 colspan。 也可以返回一個(gè)鍵名為 rowspan 和 colspan 的對(duì)象。
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3如何解決各場(chǎng)景l(fā)oading過度(避免白屏尷尬!)
在開發(fā)的過程中點(diǎn)擊提交按鈕,或者是一些其它場(chǎng)景總會(huì)遇到loading加載,下面這篇文章主要給大家介紹了關(guān)于vue3如何解決各場(chǎng)景l(fā)oading過度的相關(guān)資料,避免白屏尷尬,需要的朋友可以參考下2023-03-03vue?require.context()的用法實(shí)例詳解
require.context是webpack提供的一個(gè)api,通常用于批量注冊(cè)組件,下面這篇文章主要給大家介紹了關(guān)于vue?require.context()用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Vue利用相反數(shù)實(shí)現(xiàn)飄窗動(dòng)畫效果
飄窗,即一個(gè)類似小窗子的在網(wǎng)頁上移動(dòng)的矩形元素,通常被用于一些通知類的應(yīng)用場(chǎng)景。本文將利用相反數(shù)實(shí)現(xiàn)這一動(dòng)畫效果,需要的可以參考一下2022-05-05vue2.0實(shí)現(xiàn)檢測(cè)無用的代碼并刪除
這篇文章主要介紹了vue2.0實(shí)現(xiàn)檢測(cè)無用的代碼并刪除方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Vue項(xiàng)目啟動(dòng)后如何在瀏覽器自動(dòng)打開
這篇文章主要介紹了Vue項(xiàng)目啟動(dòng)后如何在瀏覽器自動(dòng)打開問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08vue emit之Property or method “$$v“ i
這篇文章主要介紹了vue emit之Property or method “$$v“ is not defined的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06對(duì)vue事件的延遲執(zhí)行實(shí)例講解
今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08