100行代碼實(shí)現(xiàn)一個(gè)vue分頁(yè)組功能
今天用vue來(lái)實(shí)現(xiàn)一個(gè)分頁(yè)組件,總體來(lái)說(shuō),vue實(shí)現(xiàn)比較簡(jiǎn)單,樣式部分模仿了elementUI。所有代碼的源碼可以再github上下載的到:下載地址 先來(lái)看一下實(shí)現(xiàn)效果:
整體思路
我們先看一下使用到的文件的目錄:
我們?cè)?code> pageComponentsTest.vue 頁(yè)面引入了 pageComponent.vue
分頁(yè)組件。整體思路是通過(guò) props
來(lái)達(dá)到組件的靈活通用的效果,整體語(yǔ)法是使用vue的VM語(yǔ)法。
pageComponent.vue實(shí)現(xiàn)
首先實(shí)現(xiàn)一個(gè)分頁(yè),需要知道數(shù)據(jù)總條數(shù),一個(gè)頁(yè)面顯示的數(shù)據(jù)條數(shù)和當(dāng)前顯示第幾頁(yè)的數(shù)據(jù)。那么我們?cè)?pageComponent.vue
里面的 props 就有了??聪旅娴拇a:
props: { // 分頁(yè)配置 pageConfig: { type: Object, require: true, default() { return { pageSize: 10, //一頁(yè)的數(shù)據(jù)條數(shù) pageNo: 0, //當(dāng)前頁(yè)的索引 total: 0, //總的數(shù)據(jù)條數(shù) pageTotal: 0 //總的頁(yè)數(shù) } } }
根據(jù)用戶入?yún)ⅲ覀兛梢允褂糜?jì)算屬性來(lái)計(jì)算一個(gè)總頁(yè)數(shù)的變量:
computed: { //計(jì)算總頁(yè)數(shù),如果傳了pageTotal,直接取pageTotal的值,如果傳了total,那么根據(jù)pageSize去計(jì)算 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 } } } }
有了總頁(yè)數(shù),和當(dāng)前頁(yè),就需要各種判斷來(lái)實(shí)現(xiàn)我們的html部分了,這里分4中情況
- 總頁(yè)數(shù)小于8,只需要直接遍歷到8就行了。
- 總頁(yè)數(shù)大于8,但當(dāng)前頁(yè)小于4的。
- 總頁(yè)數(shù)大于8,當(dāng)前頁(yè)靠后的。
- 總頁(yè)數(shù)大于8,當(dāng)前頁(yè)在中間的。
下面看具體的實(shí)現(xiàn):
<!--上一頁(yè)--> <button @click="prePage" :disabled="currentPage === 1">上一頁(yè)</button> <!--總頁(yè)數(shù)小于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> <!--下一頁(yè)--> <button @click="nextPage" :disabled="currentPage === pageTotal">下一頁(yè)</button>
可以看到頁(yè)面上需要實(shí)現(xiàn)3個(gè)方法,分別是上下頁(yè),和點(diǎn)擊頁(yè)面的方法。
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 的大致實(shí)現(xiàn)了,每次頁(yè)面改變,都會(huì)觸發(fā)一個(gè) changeCurrentPage 方法的回調(diào),用來(lái)通知當(dāng)前使用組件的頁(yè)面當(dāng)前頁(yè)已經(jīng)改變。
pageComponentsTest.vue的實(shí)現(xiàn)
引用頁(yè)面就比較簡(jiǎn)單了,只要傳入組件需要的對(duì)應(yīng)的參數(shù),就能顯示我們的組件了。 引用部分:
<template> <div class="pageComponentsTest"> <page-component :page-config="pageConfigTotal" @changeCurrentPage="changePage"></page-component> <page-component :page-config="pageConfigPageTotal"></page-component> </div> </template>
配合入?yún)⒉糠郑?/p>
{ 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 } } }
總結(jié)
可以看到使用vue實(shí)現(xiàn)分頁(yè)組件整體來(lái)說(shuō)是很容易了,比使用jQuery方便很多,使用vm模式開(kāi)發(fā)前端的最明顯的一個(gè)好處是,能是數(shù)據(jù)mode部分與view頁(yè)面部分保持同步,而開(kāi)發(fā)者不用考慮這個(gè)過(guò)程,所以整體來(lái)說(shuō)簡(jiǎn)單了很多。
以上所述是小編給大家介紹的100行代碼實(shí)現(xiàn)一個(gè)vue分頁(yè)組功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
VantUI封裝自定義Tabbar路由跳轉(zhuǎn)的實(shí)現(xiàn)
本文主要介紹了VantUI封裝自定義Tabbar路由跳轉(zhuǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Vue.js 實(shí)現(xiàn)地址管理頁(yè)面思路詳解(地址添加、編輯、刪除和設(shè)置默認(rèn)地址)
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)地址管理頁(yè)面的思路(地址添加、編輯、刪除和設(shè)置默認(rèn)地址),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12mpvue將vue項(xiàng)目轉(zhuǎn)換為小程序
這篇文章主要介紹了mpvue將vue項(xiàng)目轉(zhuǎn)換為小程序的相關(guān)資料及mpvue開(kāi)發(fā)流程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09使用webpack手動(dòng)搭建vue項(xiàng)目的步驟
這篇文章主要介紹了從零使用webpack手動(dòng)搭建vue項(xiàng)目的步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03前端vue中實(shí)現(xiàn)文件下載的幾種方法總結(jié)
這篇文章主要介紹了前端vue中實(shí)現(xiàn)文件下載的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04深入理解Vue.js3中Reactive的實(shí)現(xiàn)
reactive是Vue 3的Composition API中的一個(gè)函數(shù),它允許你創(chuàng)建一個(gè)響應(yīng)式的數(shù)據(jù)對(duì)象,本文主要介紹了深入理解Vue.js3中Reactive的實(shí)現(xiàn),感興趣的可以了解一下2024-01-01vue開(kāi)發(fā)中的base和publicPath的區(qū)別
本文主要介紹了vue開(kāi)發(fā)中的base和publicPath的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07