Vue2.5 結(jié)合 Element UI 之 Table 和 Pagination 組件實(shí)現(xiàn)分頁(yè)功能
2017年底了,總結(jié)了這一年多來(lái)的前端之路,Vue從入門到放棄,再二進(jìn)宮,從 Vue1.0 持續(xù)跟蹤到 Vue2.5。結(jié)合公司的一些實(shí)際項(xiàng)目,也封裝了一些比較實(shí)用的組件。
由于現(xiàn)在公司管理平臺(tái)主要運(yùn)用Element UI,索性就結(jié)合組件Table 和 Pagination 封裝了一個(gè)支持頁(yè)面切換的Table組件,不啰嗦,直接上代碼。
2、實(shí)現(xiàn)思路
2.1、Element UI 引入(整體引入)
main.js
// Element UI import Element from 'element-ui' // 默認(rèn)樣式 import 'element-ui/lib/theme-chalk/index.css'
2.2、開(kāi)始封裝 iTable.vue 組件 (骨架)
由于公司項(xiàng)目都是以 i 開(kāi)頭,所以,為了區(qū)分組件和頁(yè)面,習(xí)慣于組件命名也以 i 開(kāi)頭。 首先把 Table 、 Pagination 組件加進(jìn)來(lái)
<template> <div class="table"> <!--region 表格--> <el-table id="iTable"></el-table> <!--endregion--> <!--region 分頁(yè)--> <el-pagination></el-pagination> <!--endregion--> </div> <template>
養(yǎng)成寫注釋的好習(xí)慣,個(gè)人項(xiàng)目的注釋量基本上不會(huì)低于 30%
2.3、在頁(yè)面中引用 iTable 組件,并且給 iTable 組件傳值
<template> <div class="table-page"> <i-table :list="list" :total="total" :otherHeight="otherHeight" @handleSizeChange="handleSizeChange" @handleIndexChange="handleIndexChange" @handleSelectionChange="handleSelectionChange" :options="options" :columns="columns" :operates="operates" @handleFilter="handleFilter" @handelAction="handelAction"> </i-table> </div> </template> <script> import iTable from '../../components/Table/Index' export default { components: {iTable}, data () { return { total: 0, // table數(shù)據(jù)總條數(shù) list: [], // table數(shù)據(jù) otherHeight: 208, // 除了table表格之外的高度,為了做table表格的高度自適應(yīng) page: 1, // 當(dāng)前頁(yè)碼 limit: 20, // 每頁(yè)數(shù)量 options: { stripe: true, // 是否為斑馬紋 table loading: false, // 是否添加表格loading加載動(dòng)畫 highlightCurrentRow: true, // 是否支持當(dāng)前行高亮顯示 mutiSelect: true, // 是否支持列表項(xiàng)選中功能 filter: false, // 是否支持?jǐn)?shù)據(jù)過(guò)濾功能 action: false // 是否支持 表格操作功能 }, // table 的參數(shù) columns: [ { prop: 'id', label: '編號(hào)', align: 'center', width: 60 }, { prop: 'title', label: '標(biāo)題', align: 'center', width: 400, formatter: (row, column, cellValue) => { return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>` } }, { prop: 'state', label: '狀態(tài)', align: 'center', width: '160', render: (h, params) => { return h('el-tag', { props: {type: params.row.state === 0 ? 'success' : params.row.state === 1 ? 'info' : 'danger'} // 組件的props }, params.row.state === 0 ? '上架' : params.row.state === 1 ? '下架' : '審核中') } }, …… ], // 需要展示的列 operates: { width: 200, fixed: 'right', list: [ { label: '編輯', type: 'warning', show: true, icon: 'el-icon-edit', plain: true, disabled: true, method: (index, row) => { this.handleEdit(index, row) } }, { label: '刪除', type: 'danger', icon: 'el-icon-delete', show: true, plain: false, disabled: false, method: (index, row) => { this.handleDel(index, row) } } ] } // 列操作按鈕 } }, methods: { // 切換每頁(yè)顯示的數(shù)量 handleSizeChange (size) { this.limit = size console.log(' this.limit:', this.limit) }, // 切換頁(yè)碼 handleIndexChange (index) { this.page = index console.log(' this.page:', this.page) }, // 選中行 handleSelectionChange (val) { console.log('val:', val) }, // 編輯 handleEdit (index, row) { console.log(' index:', index) console.log(' row:', row) }, // 刪除 handleDel (index, row) { console.log(' index:', index) console.log(' row:', row) } } } </script>
除了 columns 參數(shù)和 operates 參數(shù) 之外,其它的參數(shù)應(yīng)該還好理解,好的。那我們就詳細(xì)的解釋下這兩個(gè)參數(shù),那么我們就需要結(jié)合組件iTable.vue 來(lái)講解了,接下來(lái)就給 iTable.vue 添加肌肉和血管,代碼都貼了。 比較難理解的就是columns里面的 render 參數(shù),使用了Vue的虛擬標(biāo)簽,為了就是能夠在 table 表格的列中隨心所欲的使用各種html標(biāo)簽 和 element UI 的其他組件。( 你也可以直接寫,看看 table 組件是否能識(shí)別,呵呵噠! )這個(gè)估計(jì)對(duì)于剛?cè)腴T的小伙伴是一個(gè)比較難理解的地方,詳細(xì)的大家可以先看下vue 的render,解釋的更清楚,如果有的小伙伴不理解,可以直接私信我~~~
<!--region 封裝的分頁(yè) table--> <template> <div class="table"> <el-table id="iTable" v-loading.iTable="options.loading" :data="list" :max-height="height" :stripe="options.stripe" ref="mutipleTable" @selection-change="handleSelectionChange"> <!--region 選擇框--> <el-table-column v-if="options.mutiSelect" type="selection" style="width: 55px;"> </el-table-column> <!--endregion--> <!--region 數(shù)據(jù)列--> <template v-for="(column, index) in columns"> <el-table-column :prop="column.prop" :label="column.label" :align="column.align" :width="column.width"> <template slot-scope="scope"> <template v-if="!column.render"> <template v-if="column.formatter"> <span v-html="column.formatter(scope.row, column)"></span> </template> <template v-else> <span>{{scope.row[column.prop]}}</span> </template> </template> <template v-else> <expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom> </template> </template> </el-table-column> </template> <!--endregion--> <!--region 按鈕操作組--> <el-table-column ref="fixedColumn" label="操作" align="center" :width="operates.width" :fixed="operates.fixed" v-if="operates.list.filter(_x=>_x.show === true).length > 0"> <template slot-scope="scope"> <div class="operate-group"> <template v-for="(btn, key) in operates.list"> <div class="item" v-if="btn.show"> <el-button :type="btn.type" size="mini" :icon="btn.icon" :disabled="btn.disabled" :plain="btn.plain" @click.native.prevent="btn.method(key,scope.row)">{{ btn.label }} </el-button> </div> </template> </div> </template> </el-table-column> <!--endregion--> </el-table> <div style="height:12px"></div> <!--region 分頁(yè)--> <el-pagination @size-change="handleSizeChange" @current-change="handleIndexChange" :page-size="pageSize" :page-sizes="[10, 20, 50]" :current-page="pageIndex" layout="total,sizes, prev, pager, next,jumper" :total="total"></el-pagination> <!--endregion--> <!--region 數(shù)據(jù)篩選--> <div class="filter-data fix-right" v-show="options.filter" @click="showfilterDataDialog"> <span>篩選過(guò)濾</span> </div> <!--endregion--> <!--region 表格操作--> <div class="table-action fix-right" v-show="options.action" @click="showActionTableDialog"> <span>表格操作</span> </div> <!--endregion--> </div> </template> <!--endregion--> <script> export default { props: { list: { type: Array, default: [] }, // 數(shù)據(jù)列表 columns: { type: Array, default: [] }, // 需要展示的列 === prop:列數(shù)據(jù)對(duì)應(yīng)的屬性,label:列名,align:對(duì)齊方式,width:列寬 operates: { type: Array, default: [] }, // 操作按鈕組 === label: 文本,type :類型(primary / success / warning / danger / info / text),show:是否顯示,icon:按鈕圖標(biāo),plain:是否樸素按鈕,disabled:是否禁用,method:回調(diào)方法 total: { type: Number, default: 0 }, // 總數(shù) pageSize: { type: Number, default: 20 }, // 每頁(yè)顯示的數(shù)量 otherHeight: { type: Number, default: 160 }, // 用來(lái)計(jì)算表格的高度 options: { type: Object, default: { stripe: false, // 是否為斑馬紋 table highlightCurrentRow: false // 是否要高亮當(dāng)前行 }, filter: false, action: false } // table 表格的控制參數(shù) }, components: { expandDom: { functional: true, props: { row: Object, render: Function, index: Number, column: { type: Object, default: null } }, render: (h, ctx) => { const params = { row: ctx.props.row, index: ctx.props.index } if (ctx.props.column) params.column = ctx.props.column return ctx.props.render(h, params) } } }, data () { return { pageIndex: 1, multipleSelection: [] // 多行選中 } }, mounted () { }, computed: { height () { return this.$utils.Common.getWidthHeight().height - this.otherHeight } }, methods: { // 切換每頁(yè)顯示的數(shù)量 handleSizeChange (size) { this.$emit('handleSizeChange', size) this.pageIndex = 1 }, // 切換頁(yè)碼 handleIndexChange (index) { this.$emit('handleIndexChange', index) this.pageIndex = index }, // 多行選中 handleSelectionChange (val) { this.multipleSelection = val this.$emit('handleSelectionChange', val) }, // 顯示 篩選彈窗 showfilterDataDialog () { this.$emit('handleFilter') }, // 顯示 表格操作彈窗 showActionTableDialog () { this.$emit('handelAction') } } } </script> <style lang="less" rel="stylesheet/less"> @import "../../assets/styles/mixins"; .table { height: 100%; .el-pagination { float: right; margin: 20px; } .el-table__header-wrapper, .el-table__fixed-header-wrapper { thead { tr { th { color: #333333; } } } } .el-table-column--selection .cell { padding: 0; text-align: center; } .el-table__fixed-right { bottom: 0 !important; right: 6px !important; z-index: 1004; } .operate-group { display: flex; flex-wrap: wrap; .item { margin-top: 4px; margin-bottom: 4px; display: block; flex: 0 0 50%; } } .filter-data { top: e("calc((100% - 100px) / 3)"); background-color: rgba(0, 0, 0, 0.7); } .table-action { top: e("calc((100% - 100px) / 2)"); background-color: rgba(0, 0, 0, 0.7); } .fix-right { position: absolute; right: 0; height: 100px; color: #ffffff; width: 30px; display: block; z-index: 1005; writing-mode: vertical-rl; text-align: center; line-height: 28px; border-bottom-left-radius: 6px; border-top-left-radius: 6px; cursor: pointer; } } </style>
總結(jié)
以上所述是小編給大家介紹的Vue2.5 結(jié)合 Element UI 之 Table 和 Pagination 組件實(shí)現(xiàn)分頁(yè)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
淺析vue 函數(shù)配置項(xiàng)watch及函數(shù) $watch 源碼分享
這篇文章主要介紹了vue 函數(shù)配置項(xiàng)watch及函數(shù) $watch 源碼分享 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11VSCode使React?Vue代碼調(diào)試變得更爽
這篇文章主要為大家介紹了VSCode使React?Vue代碼調(diào)試變得更爽的使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue.js 解決v-model讓select默認(rèn)選中不生效的問(wèn)題
這篇文章主要介紹了vue.js 解決v-model讓select默認(rèn)選中不生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue3與webpack5安裝element-plus樣式webpack編譯報(bào)錯(cuò)問(wèn)題解決
這篇文章主要介紹了vue3與webpack5安裝element-plus樣式webpack編譯報(bào)錯(cuò),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Vue監(jiān)聽(tīng)頁(yè)面刷新和關(guān)閉功能
我在做項(xiàng)目的時(shí)候,有一個(gè)需求,在離開(kāi)(跳轉(zhuǎn)或者關(guān)閉)購(gòu)物車頁(yè)面或者刷新購(gòu)物車頁(yè)面的時(shí)候向服務(wù)器提交一次購(gòu)物車商品數(shù)量的變化。這篇文章主要介紹了vue監(jiān)聽(tīng)頁(yè)面刷新和關(guān)閉功能,需要的朋友可以參考下2019-06-06vue簡(jiǎn)單封裝axios插件和接口的統(tǒng)一管理操作示例
這篇文章主要介紹了vue簡(jiǎn)單封裝axios插件和接口的統(tǒng)一管理操作,結(jié)合具體實(shí)例形式分析了vue中axios插件安裝、配置及接口統(tǒng)一管理具體操作技巧,需要的朋友可以參考下2020-02-02