您好,登錄后才能下訂單哦!
序言
項(xiàng)目需要自己寫(xiě)了一個(gè)基于vue的paginator分享出來(lái),歡迎各路好漢來(lái)指教
當(dāng)頁(yè)數(shù)小于999(包括999)頁(yè)
頁(yè)數(shù)大于999頁(yè)
首頁(yè)或尾頁(yè)disabled
10頁(yè)之內(nèi)顯示
Usage
參數(shù)
pageCount: 整數(shù),代表總頁(yè)數(shù)
監(jiān)聽(tīng)事件
@togglePage: 監(jiān)聽(tīng)切換頁(yè)面事件,可以獲取到當(dāng)前前往頁(yè)的頁(yè)數(shù)
父組件調(diào)用方法 index.vue
<template lang="html"> <div> <paginator :pageCount="pageCount" @togglePage="togglePage($event)"></paginator> </div> </template> <script> export default { data(){ return{ //總頁(yè)數(shù) pageCount: 24 } }, methods:{ togglePage(indexPage){ //打印出當(dāng)前頁(yè)數(shù) console.log(indexPage); }, } } </script>
分頁(yè)組件 paginator.vue
<template lang="html"> <div class="pagination"> <ul> <li :class="{disabled: curPage == 1}" @click="prevPage" v-if="pageCount > 1">上一頁(yè)</li> <li :class="{active: curPage == 1}" @click="page(1)">1</li> <li class="ellipsis" v-show="curPage > 5 && pageCount > 10">...</li> <li :class="{active: curPage == index+offset}" v-for="(item,index) in middlePages" @click="page(index+offset)">{{index+offset}}</li> <li class="ellipsis" v-show="curPage < bigLimit && pageCount > 10">...</li> <li :class="{active: curPage == pageCount}" @click="page(pageCount)" v-if="pageCount > 1">{{pageCount}}</li> <li :class="{disabled: curPage == pageCount}" @click="nextPage" v-if="pageCount > 1">下一頁(yè)</li> </ul> </div> </template> <script> export default { props:['pageCount'], data(){ return { curPage: 1, }; }, computed:{ middlePages(){ if(this.pageCount <= 2){ return 0; }else if(this.pageCount> 2 && this.pageCount <= 10){ return this.pageCount-2; }else{ return this.curPage > 999 ? 5 : 8; } }, bigLimit(){ return this.middlePages > 5 ? this.pageCount-6 : this.pageCount -3; }, offset(){ if(this.curPage <= 5){ return 2; }else if(this.curPage >= this.bigLimit){ return this.bigLimit-2; }else{ return this.middlePages > 5 ? this.curPage-3 : this.curPage-2; } } }, methods:{ page(indexPage){ this.$emit('togglePage',indexPage); this.curPage = indexPage; }, prevPage(){ if(this.curPage != 1){ this.page(this.curPage-1); } }, nextPage(){ if(this.curPage != this.pageCount){ this.page(this.curPage+1); } } } }; </script> <style lang="css" scoped> @import 'styles/vars.css'; .pagination{ width: 660px; text-align: center; ul{ margin: 40px 0 60px 0; li{ cursor: pointer; display: inline-block; padding: 5px 9px; border: 1px solid #e1e1eb; margin-right: 5px; &.active{ background: #4078c0; color: #fff; } &.ellipsis{ border: none; } &.disabled{ color: #dcdcdc; } } } } </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。