100行程式碼實現一個vue分頁元件
今天用vue來實現一個分頁元件,總體來說,vue實現比較簡單,樣式部分模仿了elementUI。所有程式碼的原始碼可以再github上下載的到: ofollow,noindex">下載地址 先來看一下實現效果:

整體思路
我們先看一下使用到的檔案的目錄:

pageComponentsTest.vue
頁面引入了
pageComponent.vue
分頁元件。整體思路是通過
props
來達到元件的靈活通用的效果,整體語法是使用vue的VM語法。
pageComponent.vue實現
首先實現一個分頁,需要知道資料總條數,一個頁面顯示的資料條數和當前顯示第幾頁的資料。那麼我們在 pageComponent.vue
裡面的 props
就有了。看下面的程式碼:
props: { // 分頁配置 pageConfig: { type: Object, require: true, default() { return { pageSize: 10,//一頁的資料條數 pageNo: 0,//當前頁的索引 total: 0,//總的資料條數 pageTotal: 0//總的頁數 } } } 複製程式碼
根據使用者入參,我們可以使用計算屬性來計算一個總頁數的變數:
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中情況
- 總頁數小於8,只需要直接遍歷到8就行了。
- 總頁數大於8,但當前頁小於4的。
- 總頁數大於8,當前頁靠後的。
- 總頁數大於8,當前頁在中間的。 下面看具體的實現:
<!--上一頁--> <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個方法,分別是上下頁,和點選頁面的方法。
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
方法的回撥,用來通知當前使用元件的頁面當前頁已經改變。
pageComponentsTest.vue的實現
引用頁面就比較簡單了,只要傳入元件需要的對應的引數,就能顯示我們的元件了。 引用部分:
<template> <div class="pageComponentsTest"> <page-component :page-config="pageConfigTotal" @changeCurrentPage="changePage"></page-component> <page-component :page-config="pageConfigPageTotal"></page-component> </div> </template> 複製程式碼
配合入參部分:
{ 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頁面部分保持同步,而開發者不用考慮這個過程,所以整體來說簡單了很多。所有的原始碼都能在 git上下載 。 最後可以關注我的個人公眾號,實時檢視更多更好的文章:
