Element使用el-table組件二次封裝
前言
在vue開發(fā)中使用element-ui的el-table時一般都需要進(jìn)行封裝以便于復(fù)用,提高開發(fā)效率,減少重復(fù)代碼,這篇博客對el-table進(jìn)行簡單的二次封裝:
一、安裝引入
npm
安裝element-ui:
npm i element-ui -S
可以看文檔按需引入,這里為了方便直接全局引入了:
import Vue from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementUI from 'element-ui' // 全局引入element-ui import 'element-ui/lib/theme-chalk/index.css' // 樣式文件需要單獨引入 Vue.config.productionTip = false Vue.use(ElementUI) new Vue({ router, store, render: h => h(App) }).$mount('#app')
二、封裝功能
新建一個chris-el-table組件,遍歷表頭變量tableTitle
使用v-for
循環(huán)生成el-table-column
,使用slot
來實現(xiàn)自定義單元格:
<template> <div class="table-container"> <el-table :data="tableData" width="100%" :row-class-name="rowClassName" :height="height" :row-style="{height: `${rowHeight}px`}"> <template v-for="(item, index) in tableTitle"> <slot v-if="item.slot" :name="item.slot"></slot> <el-table-column v-else :key="index" :prop="item.property" :label="item.label" :min-width="item.minWidth ? item.minWidth : ''" :width="item.width ? item.width : ''"> </el-table-column> </template> </el-table> </div> </template> <script> export default { name: 'chris-el-table', props: { tableData: { // 表格數(shù)據(jù) type: Array, default: () => { return [] } }, tableTitle: { // 表格頭標(biāo)題 type: Array, require: true }, height: { // 表格高度 type: [Number, String], default: '100%' }, rowHeight: { // 表格行高 type: [Number, String], default: 44 } }, data () { return {} }, methods: { rowClassName (e) { return e.rowIndex % 2 === 0 ? '' : 'light-line' } } } </script>
三、樣式覆蓋
根據(jù)需要覆蓋el-table的默認(rèn)樣式:
<style scoped lang="scss"> .table-container { /deep/ .el-table { background-color: transparent; &::before { // 表格底部邊框 background: none; } tbody tr:hover > td { // 表格觸碰樣式 background-color: #F5F7FA; } } /deep/ .el-table__header-wrapper { .el-table__cell { // 表頭樣式 height: 44px; padding: 0; background: #FFFFFF; border-bottom: #EBEEF5 solid 1px !important; text-align: center; } } /deep/ .el-table__body-wrapper { &::-webkit-scrollbar { // 表格滾動條 width: 0 !important; } .el-table__row { // 表格行樣式 background-color: #F5F7FA; .el-table__cell { padding: 0; text-align: center; border-bottom: #EBEEF5 solid 1px !important; } } .light-line { // 高亮行顏色 background-color: #FFFFFF; } } } </style>
四、使用組件
直接傳入表頭數(shù)據(jù)tableTitle
和表格數(shù)據(jù)tableData
:
<chris-el-table :table-title="tableTitle" :table-data="tableData"> </chris-el-table>
表頭數(shù)據(jù)tableTitle
大概是這樣:
tableTitle: [ { label: '日期', property: 'date' }, { label: '姓名', property: 'name' }, { label: '地址', property: 'address' }, { slot: 'handle' } ]
表格數(shù)據(jù)tableData
對應(yīng)property
,大概長這樣:
tableData: [ { date: '2016-05-02', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1516 弄' } ]
需要自定義的單元格使用slot
,對el-table-column
進(jìn)行修改:
<chris-el-table :table-title="tableTitle" :table-data="tableData"> <el-table-column slot="handle" label="操作"> <template slot-scope="scope"> <el-button @click="handleClick(scope.row)">查看</el-button> </template> </el-table-column> </chris-el-table>
源碼扔在最后:https://github.com/chrischen0405/element-component-in-vue
以上就是Element使用el-table組件二次封裝的詳細(xì)內(nèi)容,更多關(guān)于Element el-table二次封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3中v-for報錯'item'is?of?type'unknown'的
在寫vue3+ts的項目,得到一個數(shù)組,需要循環(huán)展示,使用v-for循環(huán),寫完之后發(fā)現(xiàn)有個報錯,接下來通過本文給大家介紹vue3中v-for報錯?‘item‘?is?of?type?‘unknown‘的解決方法,感興趣的朋友一起看看吧2023-11-11vue攔截器實現(xiàn)統(tǒng)一token,并兼容IE9驗證功能
這篇文章主要介紹了vue攔截器實現(xiàn)統(tǒng)一token,并兼容IE9驗證功能,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04基于Vue?+?ElementUI實現(xiàn)可編輯表格及校驗
這篇文章主要給大家介紹了基于Vue?+?ElementUI?實現(xiàn)可編輯表格及校驗,文中有詳細(xì)的代碼講解和實現(xiàn)思路,講解的非常詳細(xì),有需要的朋友可以參考下2023-08-08vue?scss后綴文件background-image路徑錯誤的解決
這篇文章主要介紹了vue?scss后綴文件background-image路徑錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Vue openLayers實現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
OpenLayers是一個用于開發(fā)WebGIS客戶端的JavaScript包,最初基于BSD許可發(fā)行。OpenLayers是一個開源的項目,其設(shè)計之意是為互聯(lián)網(wǎng)客戶端提供強(qiáng)大的地圖展示功能,包括地圖數(shù)據(jù)顯示與相關(guān)操作,并具有靈活的擴(kuò)展機(jī)制2022-09-09