VUE element-ui 寫個(gè)復(fù)用Table組件的示例代碼
餓了么的table組件功能很強(qiáng)大,對(duì)于項(xiàng)目中的各種表格基本夠用,但是……個(gè)人對(duì)于它以列為單位的操作不習(xí)慣 =。=所以改成了另一種方式(我不會(huì)告訴你其實(shí)本質(zhì)沒變)。
項(xiàng)目中表格較多,所以復(fù)用性最重要
步驟一
先來個(gè)基本的表格展示
官例的tableData
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 弄' }]
table.vue
<template> <el-table :data="tableData" border> <el-table-column prop="date" label="日期"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template>
步驟二
簡化一下表格:
//table.vue <template> <el-table :data="tableData" border> <el-table-column v-for="(item,key) in tableKey" :key="key" :prop="item.value" :label="item.name"></el-table-column> </el-table> </template> <script> export default{ name: 'table', data(){ return{ tableData:[...], tableKey: [{ name: 'date', value: '日期' },{ name: '姓名', value: 'name' },{ name: '地址', value: 'address' }] } } } </script>
步驟三
復(fù)用table.vue就是————給它數(shù)據(jù)的同時(shí)告訴它我的字段名唄
新建一個(gè)父組件sl_table.vue
//sl_table.vue <template> <sl-table :tableData="tableData" :tableKey="tableKey"></sl-table> </template> <script> import Table from '@/components/table' export default{ name: 'sl-table', data(){ return { tableData: [...] tableKey: [{ name: 'date', value: '日期' },{ name: '姓名', value: 'name' },{ name: '地址', value: 'address' }] } }, components: { 'sl-table': Table } } </script>
table.vue就更簡單了
//table.vue <template> <el-table :data="tableData" border> <el-table-column v-for="(item,key) in tableKey" :key="key" :prop="item.value" :label="item.name"></el-table-column> </el-table> </template> <script> export default{ name: 'table', data(){ return{ } }, props:['tableData','tableKey'], } </script>
步驟四
可以根據(jù)需求修改table的形式
列寬度
這個(gè)較為簡單,可以直接加個(gè)屬性
//sl_table.vue ... data(){ return { tableData: [...] tableKey: [{ name: 'date', value: '日期', width: 80 },{ name: '姓名', value: 'name', width: 80 },{ name: '地址', value: 'address' }] } }, ...
table.vue
//table.vue ... <el-table-column v-for="(item,key) in tableKey" :key="key" :prop="item.value" :label="item.name" :width="item.width"></el-table-column> ...
自定義模板列
如果我們需要告訴組件哪個(gè)是自定義的列,所以添加一個(gè)屬性operate
table.vue
<el-table-column v-for="(item,key) in tableKey" v-if="!item.operate" :key="key" :prop="item.value" :label="item.name" :width="item.width"></el-table-column> <!-- 自定義 --> <el-table-column v-else> <template scope="scope"> <slot :name="item.value" :$index="scope.$index" :row="scope.row"></slot> </template> </el-table-column> //sl_table.vue <sl-table :tableData="tableData" :tableKey="tableKey"> <template slot="date" scope="scope"> <span>{{ scope.row.date | DateFilter }}</span> </template> </sl-table> ... data(){ return { tableData: [...] tableKey: [{ name: 'date', value: '日期', operate: true },{ name: '姓名', value: 'name', operate: false },{ name: '地址', value: 'address', operate: false }] } }, filters: { DateFilter(){...} } ...
表格展開行
類似寬度,只要sl_table.vue傳入一個(gè)isExpand的屬性。這里加個(gè)每次只能展開一行的效果:
//sl_table.vue <sl-table :tableData="tableData" :tableKey="tableKey" :isExpand="true" :isExpandOnly="true"> <template slot="expand" scope="scope"> {{...expand something}} </template> ... </sl-table>
table.vue
//table.vue <el-table :data="tableData" border @expand="handleExpand" ref="raw_table"> <el-table-column v-if="isExpand" type="expand"> <template scope="scope"> <slot name="expand" :$index="scope.$index" :row="scope.row"></slot> </template> </el-table-column> </el-table> ... props: ['tableData','tableKey','isExpand','isExpandOnly'], methods: { handleExpand(row,is_expand){ if(this.isExpand && this.isExpandOnly){ this.$refs.raw_table.store.states.expandRows = expanded ? [row] : [] } } }
其他的(排序、多選)操作也是類似添加。。。多不贅述。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法
- 在vue項(xiàng)目中使用element-ui的Upload上傳組件的示例
- vue element-ui之怎么封裝一個(gè)自己的組件的詳解
- 詳解VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件
- VUE2.0+Element-UI+Echarts封裝的組件實(shí)例
- vue-cli結(jié)合Element-ui基于cropper.js封裝vue實(shí)現(xiàn)圖片裁剪組件功能
- vue-cli3.0+element-ui上傳組件el-upload的使用
- vue element-ui table組件動(dòng)態(tài)生成表頭和數(shù)據(jù)并修改單元格格式 父子組件通信
- vue 基于element-ui 分頁組件封裝的實(shí)例代碼
- Vue中正確使用Element-UI組件的方法實(shí)例
相關(guān)文章
Vue如何給組件添加點(diǎn)擊事件?@click.native
這篇文章主要介紹了Vue如何給組件添加點(diǎn)擊事件?@click.native,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue props對(duì)象validator自定義函數(shù)實(shí)例
今天小編就為大家分享一篇vue props對(duì)象validator自定義函數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)與路由詳解
在vue中進(jìn)行前端網(wǎng)頁開發(fā)時(shí),通常列表數(shù)據(jù)用el-table展示,下面這篇文章主要給大家介紹了關(guān)于Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)與路由的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02Vue 封裝防刷新考試倒計(jì)時(shí)組件的實(shí)現(xiàn)
這篇文章主要介紹了Vue 封裝防刷新考試倒計(jì)時(shí)組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06離線搭建vue環(huán)境運(yùn)行項(xiàng)目完整步驟
這篇文章主要給大家介紹了關(guān)于離線搭建vue環(huán)境運(yùn)行項(xiàng)目的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-06-06vue的列表交錯(cuò)過渡實(shí)現(xiàn)代碼示例
這篇文章主要介紹了vue的列表交錯(cuò)過渡實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05vue實(shí)現(xiàn)簡單表格組件實(shí)例詳解
vue的核心思想就是組件,什么是組件呢?按照我的理解組件就是裝配頁面的零件,vue三大核心組件 路由 狀態(tài)管理,路由控制頁面的渲染,頁面由組件組成,數(shù)據(jù)有vuex進(jìn)行管理和改變。下面我會(huì)以一個(gè)簡單的案例來說2017-04-04