自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何通過vue實(shí)現(xiàn)一款簡(jiǎn)單通用的翻頁組件

開發(fā) 前端
翻到某個(gè)列表的某一頁之后,點(diǎn)擊某一項(xiàng)到編輯頁,編輯完成后希望能夠返回到跳轉(zhuǎn)之前的那一頁。這個(gè)需求如果僅僅用上面的pager組件,實(shí)現(xiàn)起來就不是很方便。也許有人會(huì)說結(jié)合vuex可以,但是這樣的話需要在state中記錄下跳轉(zhuǎn)前的頁碼。假如有很多個(gè)翻頁列表,就需要記錄多個(gè),這顯然并不優(yōu)雅。

預(yù)覽

先上一波效果圖:

 基本元素

首先,翻頁組件(以下稱“pager組件”)一般擁有的元素有:

  • 上一頁
  • ***頁
  • 中間顯示的頁碼
  • ***一頁
  • 下一頁

初始化時(shí)需要的配置有:

  • totalPage(總頁數(shù))
  • initPage(初始頁)
  • showPrev(是否顯示上一頁)
  • showNext(是否顯示下一頁)
  • showItems(中間顯示幾頁)
  • showJump(是否顯示跳轉(zhuǎn)到第幾頁)

這些可以通過vue的props來接收。

另外,pager組件本身需要有一個(gè)記錄當(dāng)前頁的currentPage,pages數(shù)組用來容納中間顯示的頁碼,jumpPage綁定輸入的跳轉(zhuǎn)頁碼。

基本實(shí)現(xiàn)

對(duì)應(yīng)的代碼為:

  1. <template> 
  2.     <div class="pager-wrapper" v-if="totalPage > 0"
  3.         <div class="pager-pages"
  4.             <a v-show="currentPage > 1 && showPrev" @click="go(currentPage - 1)">上一頁</a> 
  5.             <a :class="{active: currentPage == 1 ? true : false}" @click="go(1)">1</a> 
  6.             <strong v-show="pages[0] > 2">...</strong> 
  7.             <a v-for="page in pages" :class="{active: currentPage == page ? true : false}" @click="go(page)">{{page}}</a> 
  8.             <strong v-show="pages[pages.length-1] < totalPage - 1">...</strong> 
  9.             <a v-if="totalPage > 1" :class="{active: currentPage == totalPage ? true : false}" @click="go(totalPage)">{{totalPage}}</a> 
  10.             <a v-show="currentPage < totalPage && showNext" @click="go(currentPage + 1)">下一頁</a> 
  11.         </div> 
  12.         <div v-if="showJump" v-show="totalPage > 1" class="pager-jump"
  13.             <span>共<em class="jump-total">{{totalPage}}</em>頁 ,跳至</span> 
  14.             <input type="number" min="1" :max="totalPage" v-model="jumpPage" class="jump-input"
  15.             <span>頁</span> 
  16.             <a @click="go(jumpPage)">確定</a> 
  17.         </div> 
  18.     </div> 
  19. </template> 
  20. <script> 
  21.   export default { 
  22.         props: { 
  23.             totalPage: { // 總頁數(shù) 
  24.                 type: Number, 
  25.                 default: 1, 
  26.                 required: true 
  27.             }, 
  28.             showItems: { // 顯示出來的頁數(shù),如: 1 ... 34[5]67 ... 10 
  29.                 type: Number, 
  30.                 default: 5 
  31.             }, 
  32.             showPrev: { // 是否顯示“上一頁” 
  33.                 type: Boolean, 
  34.                 defaulttrue 
  35.             }, 
  36.             showNext: { // 是否顯示“下一頁” 
  37.                 type: Boolean, 
  38.                 defaulttrue 
  39.             }, 
  40.             showJump: { // 是否顯示“跳轉(zhuǎn)” 
  41.                 type: Boolean, 
  42.                 defaulttrue 
  43.             }, 
  44.             initPage: { 
  45.                 type: Number, 
  46.                 default: 1 
  47.             } 
  48.         }, 
  49.         data () { 
  50.             return { 
  51.                 currentPage: 0, 
  52.                 pages: [], 
  53.                 jumpPage: 0, 
  54.             } 
  55.         }, 
  56.         created () {// 初始化時(shí)currentPage賦值 
  57.             this.currentPage = this.initPage 
  58.         } 
  59.         methods: { 
  60.             go (page) { 
  61.                 if(page < 1) { 
  62.                     page = 1 
  63.                 } 
  64.                 if(page > this.totalPage) { 
  65.                     page = this.totalPage 
  66.                 } 
  67.                 if(page === this.currentPage) { 
  68.                     return 
  69.                 } 
  70.                 this.currentPage = parseInt(page,10) 
  71.                 this.$emit('go-page',{ 
  72.                     page: this.currentPage 
  73.                 }) 
  74.             } 
  75.         }, 
  76.         watch: { 
  77.             currentPage (newVal) { 
  78.                 this.jumpPage = newVal 
  79.             }, 
  80.             initPage (newVal) { 
  81.                 if(this.currentPage !== newVal) { 
  82.                     this.currentPage = newVal 
  83.                 } 
  84.             } 
  85.         } 
  86.     } 
  87. </script>  

接下來就是pages數(shù)組的值如何獲取到。由于pages始終是跟當(dāng)前頁currentPage以及配置中需要顯示的showItems強(qiáng)相關(guān)的,那么完全可以將pages改為計(jì)算屬性:

  1. computed: { 
  2.     pages () { 
  3.         // 根據(jù)起始頁碼和結(jié)束頁碼得到頁碼數(shù)組 
  4.         let getPages = (start,end) => { 
  5.             if(start <= 1 || start > end || start >= this.totalPage) { 
  6.                 start = 2 
  7.             } 
  8.             if(end >= this.totalPage || end < start || end <= 1) { 
  9.                 end = this.totalPage - 1 
  10.             } 
  11.             let arr = [] 
  12.             for(let i = start; i <= end; i++) { 
  13.                 arr.push(i) 
  14.             } 
  15.             return arr 
  16.         } 
  17.         let counts = this.showItems 
  18.         if(this.totalPage < counts + 2) { 
  19.             return getPages(2,this.totalPage) 
  20.         } else { 
  21.             if(this.currentPage <= Math.ceil(counts/2)) { 
  22.                 return getPages(2,counts) 
  23.             } else if(this.currentPage >= this.totalPage - Math.floor(counts/2)) { 
  24.                 return getPages(this.totalPage + 1 - counts,this.totalPage - 1) 
  25.             } else { 
  26.                 let half = Math.ceil(counts/2) - 1 
  27.                 let end = this.currentPage + half 
  28.                 if(counts % 2 === 0) { 
  29.                     end++ 
  30.                 } 
  31.                 return getPages(this.currentPage - half,end
  32.             } 
  33.         } 
  34.     } 

 功能拓展

到這里一個(gè)普通的翻頁組件基本上就實(shí)現(xiàn)了(樣式自己可以去定制)。但是很多時(shí)候(特別是一些管理后臺(tái)),結(jié)合vue-router做成SPA,通常會(huì)有這樣的需求:

翻到某個(gè)列表的某一頁之后,點(diǎn)擊某一項(xiàng)到編輯頁,編輯完成后希望能夠返回到跳轉(zhuǎn)之前的那一頁。

這個(gè)需求如果僅僅用上面的pager組件,實(shí)現(xiàn)起來就不是很方便。也許有人會(huì)說結(jié)合vuex可以,但是這樣的話需要在state中記錄下跳轉(zhuǎn)前的頁碼。假如有很多個(gè)翻頁列表,就需要記錄多個(gè),這顯然并不優(yōu)雅。

不過因?yàn)関ue-router實(shí)現(xiàn)的優(yōu)雅,我們要滿足上面的需求也很簡(jiǎn)單:

首先props上增加mode配置,由于當(dāng)mode為params時(shí),跳轉(zhuǎn)需要知道是在哪一個(gè)路由下,所以:

  1. mode: { 
  2.     type: String, 
  3.     default'event' // 'event' | 'query' | 'params' 
  4. }, 
  5. routeName: { 
  6.     type: String 

 然后再在實(shí)際跳轉(zhuǎn)的邏輯方法go(page)里面,做點(diǎn)更改:

  1. go (page) { 
  2.     if(page < 1) { 
  3.         page = 1 
  4.     } 
  5.     if(page > this.totalPage) { 
  6.         page = this.totalPage 
  7.     } 
  8.     if(page === this.currentPage) { 
  9.         return 
  10.     } 
  11.     this.currentPage = parseInt(page,10) 
  12.     if(this.mode == 'query') { 
  13.         let query = this.$route.query 
  14.         query.page = this.currentPage 
  15.         this.$router.go({query: query}) 
  16.     } else if(this.mode == 'params') { 
  17.         let params = this.$route.params 
  18.         params.page = this.currentPage 
  19.         this.$router.go({name: this.routeName,params: params}) 
  20.     } else { 
  21.         this.$emit('go-page',{ 
  22.             page: this.currentPage 
  23.         }) 
  24.     } 

這樣基本上就完成了一個(gè)簡(jiǎn)單且通用的翻頁組件啦,接下里就是發(fā)不到倉(cāng)庫(kù)里供大家使用了。

本文最終實(shí)現(xiàn)的翻頁組件已經(jīng)發(fā)布,大家可以看一波源碼:

vue-simple-pager

總結(jié)

總體上講的比較淺顯,希望能有幫助。

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2015-11-27 09:18:11

AngularJSWeb應(yīng)用

2023-01-29 07:49:57

2025-03-05 10:01:44

2014-12-16 10:11:22

2020-12-03 09:00:02

Java外賣系統(tǒng)

2017-03-06 11:02:59

產(chǎn)品軟件Power Desig

2024-06-11 00:00:00

Java線程安全緩存組件

2023-03-31 14:51:46

CSS圖案背景開發(fā)

2021-02-20 07:02:24

Vue.js組件開發(fā)技術(shù)

2021-07-07 06:52:17

云圖word-cloud工具

2020-12-07 11:50:14

Java學(xué)習(xí)系統(tǒng)eclipse

2020-12-29 05:26:27

視頻播放器Vuevideo

2011-06-17 11:22:33

jQueryjQuery插件

2023-07-03 08:25:54

2024-08-22 12:35:37

2022-02-08 15:55:00

Vue組件庫(kù)Vue Demi

2020-12-03 09:33:58

前端開發(fā)工具

2014-06-20 10:32:42

APP上癮設(shè)計(jì)

2022-04-18 19:02:53

chrome擴(kuò)展瀏覽器

2009-05-11 15:12:03

網(wǎng)管軟件產(chǎn)品摩卡軟件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)