上次已經講解過vue
組件的實現過程,具體能夠看這篇文章:點擊訪問
今天用vue來實現一個分頁組件,整體來講,vue實現比較簡單,樣式部分模仿了elementUI。全部代碼的源碼能夠在github上下載的到:下載地址
先來看一下實現效果:
點擊可查看運行效果:線上地址javascript
咱們先看一下使用到的文件的目錄:
咱們在pageComponentsTest.vue
頁面引入了pageComponent.vue
分頁組件。總體思路是經過props
來達到組件的靈活通用的效果,總體語法是使用vue的VM語法。html
首先實現一個分頁,須要知道數據總條數,一個頁面顯示的數據條數和當前顯示第幾頁的數據。那麼咱們在pageComponent.vue
裏面的props
就有了。看下面的代碼:前端
props: { // 分頁配置 pageConfig: { type: Object, require: true, default() { return { pageSize: 10, //一頁的數據條數 pageNo: 0, //當前頁的索引 total: 0, //總的數據條數 pageTotal: 0 //總的頁數 } } }
根據用戶入參,咱們可使用計算屬性來計算一個總頁數的變量:vue
computed: { //計算總頁數,若是傳了pageTotal,直接取pageTotal的值,若是傳了total,那麼根據pageSize去計算 pageTotal(){ const config = this.pageConfig if(config.pageTotal){ return config.pageTotal }else { if(config.pageSize && config.total){ return Math.ceil(config.total/config.pageSize) }else { return 0 } } } }
有了總頁數,和當前頁,就須要各類判斷來實現咱們的html部分了,這裏分4種狀況:java
下面看具體的實現:git
<!--上一頁--> <button @click="prePage" :disabled="currentPage === 1">上一頁</button> <!--總頁數小於8的--> <template v-if="pageTotal <= showPageNo"> <button v-for="i in pageTotal" @click="changeCurrentPage(i)" :class="{active:i === currentPage}" :key="i">{{i}}</button> </template> <template v-else-if="currentPage < 4"> <button v-for="i in 6" @click="changeCurrentPage(i)" :class="{active:i === currentPage}" :key="i">{{i}}</button> <button :disabled="true">···</button> <button>{{pageTotal}}</button> </template> <template v-else-if="currentPage > pageTotal - 4"> <button>1</button> <button :disabled="true">···</button> <button v-for="i in 6" @click="changeCurrentPage(i + (pageTotal - 6))" :class="{active:(i + (pageTotal - 6)) === currentPage}" :key="i">{{i + (pageTotal - 6)}}</button> </template> <template v-else> <button>1</button> <button :disabled="true">···</button> <button @click="changeCurrentPage(currentPage - 2)">{{currentPage - 2}}</button> <button @click="changeCurrentPage(currentPage - 1)">{{currentPage - 1}}</button> <button class="active">{{currentPage}}</button> <button @click="changeCurrentPage(currentPage + 1)">{{currentPage + 1}}</button> <button @click="changeCurrentPage(currentPage + 2)">{{currentPage + 2}}</button> <button :disabled="true">···</button> <button @click="changeCurrentPage(pageTotal)">{{pageTotal}}</button> </template> <!--下一頁--> <button @click="nextPage" :disabled="currentPage === pageTotal">下一頁</button>
能夠看到頁面上須要實現3個方法,分別是上下頁,和點擊頁面的方法。github
methods: { prePage(){ this.currentPage -= 1 this.$emit('changeCurrentPage',this.currentPage) }, nextPage(){ this.currentPage += 1 this.$emit('changeCurrentPage',this.currentPage) }, changeCurrentPage(i){ this.currentPage = i this.$emit('changeCurrentPage',this.currentPage) } }
以上就是pageComponent.vue
的大體實現了,每次頁面改變,都會觸發一個changeCurrentPage
方法的回調,用來通知當前使用組件的頁面當前頁已經改變。學習
引用頁面就比較簡單了,只要傳入組件須要的對應的參數,就能顯示咱們的組件了。
引用部分:ui
<template> <div class="pageComponentsTest"> <page-component :page-config="pageConfigTotal" @changeCurrentPage="changePage"></page-component> <page-component :page-config="pageConfigPageTotal"></page-component> </div> </template>
配合入參部分:this
{ name: "pageComponentsTest", data() { return { pageConfigTotal:{total:21,pageSize:10,pageNo:1}, pageConfigPageTotal:{total:21,pageSize:10,pageNo:1,pageTotal:50} } }, components:{'page-component':pageComponent}, methods: { changePage(page){ this.pageConfigTotal.pageNo = page } } }
能夠看到使用vue
實現分頁組件總體來講是很容易了,比使用jQuery
方便不少,使用vm模式開發前端的最明顯的一個好處是,能是數據mode
部分與view
頁面部分保持同步,而開發者不用考慮這個過程,因此總體來講簡單了不少。
線上體驗地址
全部的源碼均可以在個人倉庫地址:下載
我的博客:訪問
公衆號:
學習如逆水行舟,不進則退,前端技術飛速發展,若是天天不堅持學習,就會跟不上,我會陪着你們,天天堅持推送博文,跟你們一同進步,但願你們能關注我,第一時間收到最新文章。