Element使用el-table組件二次封裝
前言
在vue開發(fā)中使用element-ui的el-table時一般都需要進行封裝以便于復(fù)用,提高開發(fā)效率,減少重復(fù)代碼,這篇博客對el-table進行簡單的二次封裝:
一、安裝引入
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: { // 表格頭標題
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的默認樣式:
<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進行修改:
<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組件二次封裝的詳細內(nèi)容,更多關(guān)于Element el-table二次封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue實現(xiàn)動態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法
下面小編就為大家分享一篇Vue實現(xiàn)動態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
IE11下處理Promise及Vue的單項數(shù)據(jù)流問題
最近我開發(fā)的公司的競賽網(wǎng)站被發(fā)現(xiàn)在IE11下排行榜無數(shù)據(jù),但是在其他瀏覽器沒問題,我然后打開控制臺一看,發(fā)現(xiàn)有問題,糾結(jié)了半天才搗騰好,下面小編把方案分享處理,供大家選擇2019-07-07
vue中的vue-router?query方式和params方式詳解
這篇文章主要介紹了vue中的vue-router?query方式和params方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue3中實現(xiàn)文本顯示省略號和tooltips提示框的方式詳解
在?B?端業(yè)務(wù)中,我們經(jīng)常會遇到文本內(nèi)容超出容器區(qū)域需顯示省略號的需求,當(dāng)鼠標移入文本時,會出現(xiàn)?Tooltip?顯示完整內(nèi)容,最近,我也遇到了這樣的場景,接下來給大家介紹vue3中實現(xiàn)文本顯示省略號和tooltips提示框的方式,需要的朋友可以參考下2024-04-04

