基于Element Plus的TableColumnGroup 組件使用詳解
在 Element Plus 的表格組件中(ElTable),你可以使用 TableColumnGroup 來(lái)創(chuàng)建一個(gè)列組,這個(gè)列組可以包含多個(gè)列(ElTableColumn)。這樣,你可以將一些列組織在一起,形成一個(gè)邏輯上的“組”,這在視覺(jué)上可以幫助用戶更好地理解數(shù)據(jù)結(jié)構(gòu)。
在前端開發(fā)中,表格是展示數(shù)據(jù)的常用組件,而復(fù)雜的業(yè)務(wù)場(chǎng)景往往需要用到多級(jí)表頭。本文將介紹一款基于 Element Plus 封裝的 TableColumnGroup 組件,該組件支持嵌套列組、自定義渲染、條件渲染等功能,能滿足大多數(shù)復(fù)雜表格的展示需求。
組件功能特點(diǎn)
- 支持多級(jí)嵌套列組,輕松實(shí)現(xiàn)復(fù)雜表頭結(jié)構(gòu)
- 提供靈活的單元格渲染方式:默認(rèn)渲染、自定義組件渲染、條件渲染
- 支持表格列的固定、寬度設(shè)置、對(duì)齊方式配置
- 內(nèi)置數(shù)字格式化功能,支持千分制顯示和單位轉(zhuǎn)換
- 支持自定義樣式類名,方便進(jìn)行樣式調(diào)整
組件參數(shù)說(shuō)明
參數(shù)名 | 類型 | 默認(rèn)值 | 說(shuō)明 |
label | String | - | 列組的標(biāo)題 |
type | String | '' | 列的類型,同 Element Plus 的 el-table-column |
className | String | - | 列組的樣式類名 |
columns | Array | [] | 普通列配置數(shù)組,當(dāng)沒(méi)有嵌套列組時(shí)使用 |
nestedGroups | Array | [] | 嵌套列組配置數(shù)組 |
groupFixed | String/Boolean | null | 列組是否固定,同 Element Plus 的 fixed 屬性 |
groupWidth | String/Number | - | 列組的寬度 |
groupProp | String | - | 列組對(duì)應(yīng)的屬性名 |
treeProps | Object | null | 樹形表格配置 |
rowKey | String | - | 行數(shù)據(jù)的唯一標(biāo)識(shí) |
columns 數(shù)組元素配置
columns 數(shù)組中的每個(gè)元素可配置以下屬性:
屬性名 | 類型 | 說(shuō)明 |
align | String | 單元格內(nèi)容對(duì)齊方式,默認(rèn) 'center' |
label | String | 列標(biāo)題 |
prop | String | 對(duì)應(yīng)行數(shù)據(jù)的屬性名 |
width | String/Number | 列寬度 |
className | String | 列的樣式類名,默認(rèn)使用父組件的 className |
fixed | String/Boolean | 列是否固定 |
customRender | Component | 自定義渲染組件 |
customRenderProps | Object | 傳遞給自定義渲染組件的屬性 |
conditionalRender | Function | 條件渲染函數(shù),返回布爾值決定是否顯示內(nèi)容 |
unit | String | 數(shù)據(jù)單位,用于格式化顯示 |
使用示例
1. 基礎(chǔ)使用(普通列)
<template>
<el-table :data="tableData">
<TableColumnGroup
:columns="[
{ label: '姓名', prop: 'name' },
{ label: '年齡', prop: 'age', unit: '歲' },
{ label: '郵箱', prop: 'email' }
]"
/>
</el-table>
</template>2. 嵌套列組
<template>
<el-table :data="tableData">
<TableColumnGroup label="基本信息">
<TableColumnGroup
:columns="[
{ label: '姓名', prop: 'name' },
{ label: '年齡', prop: 'age', unit: '歲' }
]"
/>
</TableColumnGroup>
<TableColumnGroup label="聯(lián)系方式">
<TableColumnGroup
:nestedGroups="[
{
label: '電話',
columns: [
{ label: '手機(jī)', prop: 'phone' },
{ label: '座機(jī)', prop: 'landline' }
]
},
{
label: '網(wǎng)絡(luò)',
columns: [
{ label: '郵箱', prop: 'email' },
{ label: '網(wǎng)址', prop: 'website' }
]
}
]"
/>
</TableColumnGroup>
</el-table>
</template>3. 自定義渲染
<template>
<el-table :data="tableData">
<TableColumnGroup
:columns="[
{ label: '姓名', prop: 'name' },
{
label: '狀態(tài)',
prop: 'status',
customRender: StatusTag,
customRenderProps: { colors: { active: 'green', inactive: 'gray' } }
}
]"
/>
</el-table>
</template>
<script setup>
import StatusTag from './StatusTag.vue'
</script>4. 條件渲染
<template>
<el-table :data="tableData">
<TableColumnGroup
:columns="[
{ label: '姓名', prop: 'name' },
{
label: '業(yè)績(jī)',
prop: 'performance',
unit: '元',
conditionalRender: (row) => row.performance > 0
}
]"
/>
</el-table>
</template>完整代碼實(shí)例
<!-- TableColumnGroup.vue -->
<template>
<el-table-column align="center" :label="label" :type="type" :class-name="className" :fixed="groupFixed"
:width="groupWidth" :prop="groupProp">
<!-- 支持嵌套列組 -->
<template v-if="hasNestedGroups">
<TableColumnGroup v-for="(nestedGroup, nestedIndex) in nestedGroups" :key="nestedIndex" v-bind="nestedGroup" />
</template>
<!-- 普通列 -->
<template v-else>
<el-table-column v-for="col in columns" :key="col.prop" :align="col.align || 'center'" :label="col.label"
:prop="col.prop" :width="col.width" :class-name="col.className || className" :fixed="col.fixed">
<template #default="scope">
<span>
<!-- 支持自定義渲染 -->
<template v-if="col.customRender">
<component :is="col.customRender" :row="scope.row" :value="scope.row[col.prop]"
v-bind="col.customRenderProps" />
</template>
<template v-else-if="col.conditionalRender">
<!-- 條件渲染 -->
<span v-if="col.conditionalRender(scope.row)">
{{ formatNumberWithUnit(scope.row[col.prop], col.unit) }}
</span>
<span v-else class="hidden-column">-</span>
</template>
<template v-else>
<!-- 數(shù)字類型格式化:千分制+兩位小數(shù) -->
{{ formatNumberWithUnit(scope.row[col.prop], col.unit) }}
</template>
</span>
</template>
</el-table-column>
</template>
</el-table-column>
</template>
<script setup>
import { computed } from 'vue'
import { formatNumberWithUnit } from '@/utils/validate'
const props = defineProps({
label: String,
type: {
type: String,
default: '',
},
className: String,
// 普通列配置
columns: {
type: Array,
default: () => [],
},
// 嵌套列組配置
nestedGroups: {
type: Array,
default: () => [],
},
groupFixed: {
type: [String, Boolean],
default: null,
},
groupWidth: [String, Number],
groupProp: String,
// 樹形表格配置
treeProps: {
type: Object,
default: null,
},
rowKey: String,
})
// 判斷是否有嵌套列組
const hasNestedGroups = computed(() => {
return props.nestedGroups && props.nestedGroups.length > 0
})
</script>
<style lang="scss">
.el-table__row.level-1 {
background-color: #ffffff !important;
}
.el-table__row.level-2 {
background-color: #f8f3c3 !important;
}
.el-table__row.level-3 {
background-color: #fcd787 !important;
}
.el-table__row:hover>td {
background-color: #e9e9e9 !important;
}
.hidden-column {
color: #ccc;
font-style: italic;
}
</style>模板使用
<el-table v-loading="loading" :data="oaBusinessMetricList" border max-height="660" row-key="id"
scrollbar-always-on :fit="true" resizable @selection-change="handleSelectionChange">
<el-table-column align="center" type="selection" width="55" fixed="left" />
<!-- 使用 TableColumnGroup 組件渲染列組 -->
<TableColumnGroup v-for="group in columnGroups" :key="group.key" v-bind="group" />
<!-- 操作列 -->
<el-table-column align="center" class-name="small-padding fixed-width blueInfoHeader" fixed="right" label="操作"
width="120">
<template #default="scope">
<el-tooltip content="修改" placement="top">
<el-button v-hasPerm="['oa:oaBusinessMetric:edit']" icon="Edit" link type="primary"
@click="handleUpdate(scope.row)" />
</el-tooltip>
</template>
</el-table-column>
</el-table>const {
getColumns,
getColumnGroups,
} = useOaBusinessMetric();
// 獲取列組配置并生成列組數(shù)據(jù)
const columnGroups = computed(() =>
getColumnGroups.value.map(group => ({
...group,
columns: getColumns(group.columnKey)
}))
);樣式說(shuō)明
組件內(nèi)置了一些基礎(chǔ)樣式,可通過(guò)自定義類名進(jìn)行覆蓋:
- 不同層級(jí)行的背景色:.el-table__row.level-1、.el-table__row.level-2、.el-table__row.level-3
- 行 hover 樣式:.el-table__row:hover>td
- 條件渲染隱藏時(shí)的樣式:.hidden-column(灰色斜體)
可根據(jù)實(shí)際需求在項(xiàng)目中自定義這些樣式類。
總結(jié)
TableColumnGroup 組件通過(guò)對(duì) Element Plus 的 el-table-column 進(jìn)行二次封裝,提供了更強(qiáng)大的列組管理功能,特別適合需要展示復(fù)雜表頭結(jié)構(gòu)的業(yè)務(wù)場(chǎng)景。組件設(shè)計(jì)靈活,支持多種渲染方式,能夠滿足不同的數(shù)據(jù)展示需求。
希望本文能幫助大家更好地理解和使用這個(gè)組件,如有任何問(wèn)題或建議,歡迎在評(píng)論區(qū)留言討論。
到此這篇關(guān)于基于Element Plus的TableColumnGroup 組件使用詳解的文章就介紹到這了,更多相關(guān)Element Plus TableColumnGroup 組件使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- elementui之el-table-column日期格式顯示方式
- elementui el-table中如何給表頭 el-table-column 加一個(gè)鼠標(biāo)移入提示說(shuō)明
- el-table el-table-column表頭嵌套循環(huán)數(shù)據(jù)的示例代碼
- 使用element ui中el-table-column進(jìn)行自定義校驗(yàn)
- el-table-column疊加el-popover使用示例小結(jié)
- el-table-column 內(nèi)容不自動(dòng)換行的解決方法
- 詳解VUE 對(duì)element-ui中的ElTableColumn擴(kuò)展
相關(guān)文章
JavaScript實(shí)現(xiàn)256色轉(zhuǎn)灰度圖
本文主要介紹了JavaScript實(shí)現(xiàn)256色轉(zhuǎn)灰度圖的示例代碼,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
JSON+JavaScript處理JSON的簡(jiǎn)單例子
JSON+JavaScript處理JSON的簡(jiǎn)單例子,需要的朋友可以參考一下2013-03-03
原生態(tài)js,鼠標(biāo)按下后,經(jīng)過(guò)了那些單元格的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇原生態(tài)js,鼠標(biāo)按下后,經(jīng)過(guò)了那些單元格的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
JavaScript設(shè)計(jì)模式之抽象工廠模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之抽象工廠模式介紹,抽象工廠模式就是對(duì)功能類單獨(dú)創(chuàng)建工廠類,這樣就不必修改之前的代碼,又?jǐn)U展了功能,需要的朋友可以參考下2014-12-12
JavaScript檢查一個(gè)值是否為數(shù)字實(shí)例代碼
眾所周知在JavaScript編程中判斷輸入是否為數(shù)字類型是一個(gè)常見(jiàn)的需求,特別是在處理用戶輸入或者驗(yàn)證表單數(shù)據(jù)時(shí),這篇文章主要介紹了JavaScript檢查一個(gè)值是否為數(shù)字的相關(guān)資料,需要的朋友可以參考下2025-11-11
菜鳥也能搞懂js中typeof與instanceof區(qū)別
instanceof和typeof是兩個(gè)運(yùn)算符,在程序設(shè)計(jì)中用到,常用來(lái)判斷一個(gè)變量是否為空,或者是什么類型的,本文就來(lái)介紹一下typeof與instanceof區(qū)別,感興趣的可以了解一下2021-09-09
頁(yè)面實(shí)時(shí)更新時(shí)間的JS實(shí)例代碼
這篇文章主要介紹了頁(yè)面實(shí)時(shí)更新時(shí)間的JS實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
JavaScript類的繼承方法小結(jié)【組合繼承分析】
這篇文章主要介紹了JavaScript類的繼承方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript繼承的概念、原理及組合繼承相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07
showModalDialog在谷歌瀏覽器下會(huì)返回Null的解決方法
showModalDialog的返回值在IE、火狐下面都能夠獲取返回值,但是在谷歌瀏覽器下面會(huì)返回Null,下面有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下2013-11-11

