VUE實(shí)現(xiàn)一個(gè)分頁(yè)組件的示例
分頁(yè)是WEB開(kāi)發(fā)中很常用的功能,尤其是在各種前后端分離的今天,后端API返回?cái)?shù)據(jù),前端根據(jù)數(shù)據(jù)的count以及當(dāng)前頁(yè)碼pageIndex來(lái)計(jì)算分頁(yè)頁(yè)碼并渲染到頁(yè)面上已經(jīng)是一個(gè)很普通很常見(jiàn)的功能了。從最開(kāi)始的jquery時(shí)代到現(xiàn)在的各種各樣的前端框架時(shí)代,分頁(yè)功能都是必不可少的。
分頁(yè)大多數(shù)(基本上)情況下都是對(duì)異步數(shù)據(jù)列表的處理,這里首先需要明白一下分頁(yè)的流程。
在已知每頁(yè)顯示數(shù)據(jù)量pageSize以及當(dāng)前頁(yè)碼pageIndex的情況下:
- 請(qǐng)求API,返回第一屏數(shù)據(jù)(pageSize內(nèi))以及所有相關(guān)條件的數(shù)據(jù)總量count
- 將數(shù)據(jù)總量傳遞給page組件,來(lái)計(jì)算頁(yè)碼并渲染到頁(yè)面上
- 點(diǎn)擊頁(yè)碼,發(fā)送請(qǐng)求獲取該頁(yè)碼的數(shù)據(jù),返回?cái)?shù)據(jù)總量count以及該頁(yè)碼下的數(shù)據(jù)條目。
由于獲取數(shù)據(jù)條件的變化(假設(shè)是個(gè)搜索,關(guān)鍵詞變了),count是不定的;再或者,有個(gè)select下拉框,來(lái)控制每頁(yè)顯示的數(shù)據(jù)量pageSize,當(dāng)它變化的時(shí)候,總頁(yè)碼肯定也是要變化的。因此很多情況下要重新計(jì)算頁(yè)碼并渲染。
了解了流程,在Vue中實(shí)現(xiàn)一個(gè)分頁(yè)組件也就變得簡(jiǎn)單了。
簡(jiǎn)單處理,樣式類似于bootstrap的分頁(yè)組件,在第一頁(yè)時(shí),禁用上一頁(yè),以及首頁(yè)按鈕;在最后一頁(yè)時(shí),禁用下一頁(yè),以及尾頁(yè)按鈕;超出范圍的頁(yè)碼以…來(lái)代替,效果圖如下:
由于獲取數(shù)據(jù)條件的變化(假設(shè)是個(gè)搜索,關(guān)鍵詞變了),count是不定的;再或者,有個(gè)select下拉框,來(lái)控制每頁(yè)顯示的數(shù)據(jù)量pageSize,當(dāng)它變化的時(shí)候,總頁(yè)碼肯定也是要變化的。因此很多情況下要重新計(jì)算頁(yè)碼并渲染。
了解了流程,在Vue中實(shí)現(xiàn)一個(gè)分頁(yè)組件也就變得簡(jiǎn)單了。
簡(jiǎn)單處理,樣式類似于bootstrap的分頁(yè)組件,在第一頁(yè)時(shí),禁用上一頁(yè),以及首頁(yè)按鈕;在最后一頁(yè)時(shí),禁用下一頁(yè),以及尾頁(yè)按鈕;超出范圍的頁(yè)碼以…來(lái)代替,效果圖如下:
分頁(yè)組件
template
<template> <ul class="mo-paging"> <!-- prev --> <li :class="['paging-item', 'paging-item--prev', {'paging-item--disabled' : index === 1}]" @click="prev">prev</li> <!-- first --> <li :class="['paging-item', 'paging-item--first', {'paging-item--disabled' : index === 1}]" @click="first">first</li> <li :class="['paging-item', 'paging-item--more']" v-if="showPrevMore">...</li> <li :class="['paging-item', {'paging-item--current' : index === pager}]" v-for="pager in pagers" @click="go(pager)">{{ pager }}</li> <li :class="['paging-item', 'paging-item--more']" v-if="showNextMore">...</li> <!-- last --> <li :class="['paging-item', 'paging-item--last', {'paging-item--disabled' : index === pages}]" @click="last">last</li> <!-- next --> <li :class="['paging-item', 'paging-item--next', {'paging-item--disabled' : index === pages}]" @click="next">next</li> </ul> </template>
style(scss)
.mo-paging { display: inline-block; padding: 0; margin: 1rem 0; font-size: 0; list-style: none; user-select: none; > .paging-item { display: inline; font-size: 14px; position: relative; padding: 6px 12px; line-height: 1.42857143; text-decoration: none; border: 1px solid #ccc; background-color: #fff; margin-left: -1px; cursor: pointer; color: #0275d8; &:first-child { margin-left: 0; } &:hover { background-color: #f0f0f0; color: #0275d8; } &.paging-item--disabled, &.paging-item--more{ background-color: #fff; color: #505050; } //禁用 &.paging-item--disabled { cursor: not-allowed; opacity: .75; } &.paging-item--more, &.paging-item--current { cursor: default; } //選中 &.paging-item--current { background-color: #0275d8; color:#fff; position: relative; z-index: 1; border-color: #0275d8; } } }
javascript
export default { name : 'MoPaging', //通過(guò)props來(lái)接受從父組件傳遞過(guò)來(lái)的值 props : { //頁(yè)面中的可見(jiàn)頁(yè)碼,其他的以...替代, 必須是奇數(shù) perPages : { type : Number, default : 5 }, //當(dāng)前頁(yè)碼 pageIndex : { type : Number, default : 1 }, //每頁(yè)顯示條數(shù) pageSize : { type : Number, default : 10 }, //總記錄數(shù) total : { type : Number, default : 1 }, }, methods : { prev(){ if (this.index > 1) { this.go(this.index - 1) } }, next(){ if (this.index < this.pages) { this.go(this.index + 1) } }, first(){ if (this.index !== 1) { this.go(1) } }, last(){ if (this.index != this.pages) { this.go(this.pages) } }, go (page) { if (this.index !== page) { this.index = page //父組件通過(guò)change方法來(lái)接受當(dāng)前的頁(yè)碼 this.$emit('change', this.index) } } }, computed : { //計(jì)算總頁(yè)碼 pages(){ return Math.ceil(this.size / this.limit) }, //計(jì)算頁(yè)碼,當(dāng)count等變化時(shí)自動(dòng)計(jì)算 pagers () { const array = [] const perPages = this.perPages const pageCount = this.pages let current = this.index const _offset = (perPages - 1) / 2 const offset = { start : current - _offset, end : current + _offset } //-1, 3 if (offset.start < 1) { offset.end = offset.end + (1 - offset.start) offset.start = 1 } if (offset.end > pageCount) { offset.start = offset.start - (offset.end - pageCount) offset.end = pageCount } if (offset.start < 1) offset.start = 1 this.showPrevMore = (offset.start > 1) this.showNextMore = (offset.end < pageCount) for (let i = offset.start; i <= offset.end; i++) { array.push(i) } return array } }, data () { return { index : this.pageIndex, //當(dāng)前頁(yè)碼 limit : this.pageSize, //每頁(yè)顯示條數(shù) size : this.total || 1, //總記錄數(shù) showPrevMore : false, showNextMore : false } }, watch : { pageIndex(val) { this.index = val || 1 }, pageSize(val) { this.limit = val || 10 }, total(val) { this.size = val || 1 } } }
父組件中使用
<template> <div class="list"> <template v-if="count"> <ul> <li v-for="item in items">...</li> </ul> <mo-paging :page-index="currentPage" :totla="count" :page-size="pageSize" @change="pageChange"> </mo-paging> </template> </div> </template> <script> import MoPaging from './paging' export default { //顯示的聲明組件 components : { MoPaging }, data () { return { pageSize : 20 , //每頁(yè)顯示20條數(shù)據(jù) currentPage : 1, //當(dāng)前頁(yè)碼 count : 0, //總記錄數(shù) items : [] } }, methods : { //獲取數(shù)據(jù) getList () { //模擬 let url = `/api/list/?pageSize=${this.pageSize}¤tPage=${this.currentPage}` this.$http.get(url) .then(({body}) => { //子組件監(jiān)聽(tīng)到count變化會(huì)自動(dòng)更新DOM this.count = body.count this.items = body.list }) }, //從page組件傳遞過(guò)來(lái)的當(dāng)前page pageChange (page) { this.currentPage = page this.getList() } }, mounted() { //請(qǐng)求第一頁(yè)數(shù)據(jù) this.getList() } } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vue.js的表格分頁(yè)組件
- vue分頁(yè)組件table-pagebar使用實(shí)例解析
- 基于Vue如何封裝分頁(yè)組件
- 使用vue.js制作分頁(yè)組件
- 基于vue2的table分頁(yè)組件實(shí)現(xiàn)方法
- 基于vue實(shí)現(xiàn)swipe分頁(yè)組件實(shí)例
- vuejs2.0實(shí)現(xiàn)分頁(yè)組件使用$emit進(jìn)行事件監(jiān)聽(tīng)數(shù)據(jù)傳遞的方法
- vue2.0實(shí)現(xiàn)分頁(yè)組件的實(shí)例代碼
- vue 基于element-ui 分頁(yè)組件封裝的實(shí)例代碼
- Vue開(kāi)發(fā)之封裝分頁(yè)組件與使用示例
相關(guān)文章
VUE中攔截請(qǐng)求并無(wú)感知刷新token方式
這篇文章主要介紹了VUE中攔截請(qǐng)求并無(wú)感知刷新token方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08詳解刷新頁(yè)面vuex數(shù)據(jù)不消失和不跳轉(zhuǎn)頁(yè)面的解決
這篇文章主要介紹了詳解刷新頁(yè)面vuex數(shù)據(jù)不消失和不跳轉(zhuǎn)頁(yè)面的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Vue項(xiàng)目部署后提示刷新版本的實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目部署后提示刷新版本的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-06-06Vue一個(gè)動(dòng)態(tài)添加background-image的實(shí)現(xiàn)
這篇文章主要介紹了Vue一個(gè)動(dòng)態(tài)添加background-image的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue中LocalStorage與SessionStorage的區(qū)別與用法
本文主要介紹了LocalStorage和SessionStorage。LocalStorage和SessionStorage是兩種存儲(chǔ)方式,本文詳細(xì)的介紹一下區(qū)別以及用法,感興趣的可以了解一下2021-09-09Vue3使用transition組件改變DOM屬性的方式小結(jié)
這篇文章主要為大家詳細(xì)介紹了Vue3中使用transition組件改變DOM屬性的常用方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01vue項(xiàng)目運(yùn)行時(shí)出現(xiàn)It works的問(wèn)題解決
本文主要介紹了vue項(xiàng)目運(yùn)行時(shí)出現(xiàn)It works的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Grid拖拽布局
這篇文章主要為大家詳細(xì)介紹了如何利用vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Grid拖拽布局,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12