src/pagination.js
import Pager from './pager.vue'; import ElSelect from 'element-ui/packages/select'; import ElOption from 'element-ui/packages/option'; import ElInput from 'element-ui/packages/input'; import Locale from 'element-ui/src/mixins/locale'; import { valueEquals } from 'element-ui/src/utils/util'; export default { name: 'ElPagination', props: { // 每頁顯示條目個數,支持 .sync 修飾符 pageSize: { type: Number, default: 10 }, // 是否使用小型分頁樣式 small: Boolean, // 總條目數 total: Number, // 總頁數,total 和 page-count 設置任意一個就可以達到顯示頁碼的功能;如果要支持 page-sizes 的更改,則需要使用 total 屬性 pageCount: Number, // 頁碼按鈕的數量,當總頁數超過該值時會折疊 pagerCount: { type: Number, validator (value) { return (value | 0) === value && value > 4 && value < 22 && (value % 2) === 1; }, default: 7 }, // 當前頁數,支持 .sync 修飾符 currentPage: { type: Number, default: 1 }, // 組件布局,子組件名用逗號分隔 String sizes, prev, pager, next, jumper, ->, total, slot layout: { default: 'prev, pager, next, jumper, ->, total' }, // 每頁顯示個數選擇器的選項設置 number[] — [10, 20, 30, 40, 50, 100] pageSizes: { type: Array, default () { return [10, 20, 30, 40, 50, 100]; } }, // 每頁顯示個數選擇器的下拉框類名 popperClass: String, // 替代圖標顯示的上一頁文字 prevText: String, // 替代圖標顯示的下一頁文字 nextText: String, // 是否為分頁按鈕添加背景色 background: Boolean, // 是否禁用 disabled: Boolean, // 只有一頁時是否隱藏 hideOnSinglePage: Boolean }, data () { return { //當前的頁碼 internalCurrentPage: 1, //總頁數 internalPageSize: 0, lastEmittedPage: -1, userChangePageSize: false }; }, //render函數生成el-pagination render (h) { const layout = this.layout; if (!layout) return null; if (this.hideOnSinglePage && (!this.internalPageCount || this.internalPageCount === 1)) return null; // 最外層div let template = <div class={['el-pagination', { 'is-background': this.background, 'el-pagination--small': this.small }]}></div>; const TEMPLATE_MAP = { prev: <prev></prev>, jumper: <jumper></jumper>, pager: <pager currentPage={this.internalCurrentPage} pageCount={this.internalPageCount} pagerCount={this.pagerCount} on-change={this.handleCurrentChange} disabled={this.disabled}></pager>, next: <next></next>, sizes: <sizes pageSizes={this.pageSizes}></sizes>, slot: <slot>{this.$slots.default ? this.$slots.default : ''}</slot>, total: <total></total> }; const components = layout.split(',').map((item) => item.trim()); const rightWrapper = <div class="el-pagination__rightwrapper"></div>; let haveRightWrapper = false; template.children = template.children || []; rightWrapper.children = rightWrapper.children || []; // ->這個符號主要是將其后面的組件放在rightWrapper中,然后右浮動;如果存在->符號,就將haveRightWrapper為true components.forEach(compo => { if (compo === '->') { haveRightWrapper = true; return; } // 當haveRightWrapper為true,即在->后面的放入rightWrapper中 if (!haveRightWrapper) { template.children.push(TEMPLATE_MAP[compo]); } else { rightWrapper.children.push(TEMPLATE_MAP[compo]); } }); if (haveRightWrapper) { //將rightWrapper加在template.children數組的開頭,這樣rightWrapper浮動之后就是在最右邊 template.children.unshift(rightWrapper); } return template; }, components: { // 上一頁組件 Prev: { //上一頁; prevText用戶設置的替代上一頁圖標的文字,存在顯示文字,不存在顯示上一頁圖標 render (h) { return ( <button type="button" class="btn-prev" disabled={this.$parent.disabled || this.$parent.internalCurrentPage <= 1} on-click={this.$parent.prev}> { this.$parent.prevText ? <span>{this.$parent.prevText}</span> : <i class="el-icon el-icon-arrow-left"></i> } </button> ); } }, //下一頁組件 Next: { // 當前頁數等於總頁數時 或者 總頁數等於0時,下一頁按鈕被禁用 render (h) { return ( <button type="button" class="btn-next" disabled={this.$parent.disabled || this.$parent.internalCurrentPage === this.$parent.internalPageCount || this.$parent.internalPageCount === 0} on-click={this.$parent.next}> { this.$parent.nextText ? <span>{this.$parent.nextText}</span> : <i class="el-icon el-icon-arrow-right"></i> } </button> ); } }, // 每頁顯示條目個數組件 Sizes: { mixins: [Locale], props: { pageSizes: Array //每頁顯示個數選擇器的選項設置 [10, 20, 30, 40, 50, 100] }, watch: { pageSizes: { // 確認是否以當前的初始值執行handler的函數 immediate: true, handler (newVal, oldVal) { if (valueEquals(newVal, oldVal)) return; // 如果用戶設置了每頁顯示的條目個數,並且pageSize在設置的pageSizes中存在的話,就顯示pageSize,否則就顯示this.pageSizes[0] // 最后將每頁顯示的條目個數賦值給this.$parent.internalPageSize if (Array.isArray(newVal)) { this.$parent.internalPageSize = newVal.indexOf(this.$parent.pageSize) > -1 ? this.$parent.pageSize : this.pageSizes[0]; } } } }, render (h) { // this.t('el.pagination.pagesize') 返回'條/頁' return ( <span class="el-pagination__sizes"> <el-select value={this.$parent.internalPageSize} popperClass={this.$parent.popperClass || ''} size="mini" on-input={this.handleChange} disabled={this.$parent.disabled}> { this.pageSizes.map(item => <el-option value={item} label={item + this.t('el.pagination.pagesize')}> </el-option> ) } </el-select> </span> ); }, components: { ElSelect, ElOption }, methods: { handleChange (val) { if (val !== this.$parent.internalPageSize) { this.$parent.internalPageSize = val = parseInt(val, 10); this.$parent.userChangePageSize = true; //如果父組件中pageSize用了.sync 修飾符,這里將會觸發父組件的update,改變pageSize的值 this.$parent.$emit('update:pageSize', val); //觸發父組件的size-change事件,將改變的每頁顯示的條目個數的值傳遞出去 this.$parent.$emit('size-change', val); } } } }, //前往多少頁組件 Jumper: { mixins: [Locale], components: { ElInput }, data () { return { userInput: null }; }, watch: { '$parent.internalCurrentPage' () { this.userInput = null; } }, methods: { // 按下回車,前往多少頁 handleKeyup ({ keyCode, target }) { // Chrome, Safari, Firefox triggers change event on Enter // Hack for IE: https://github.com/ElemeFE/element/issues/11710 // Drop this method when we no longer supports IE if (keyCode === 13) { this.handleChange(target.value); } }, handleInput (value) { this.userInput = value; }, // 改變當前頁 handleChange (value) { // 更新頁碼列表中當前頁的值 this.$parent.internalCurrentPage = this.$parent.getValidCurrentPage(value); this.$parent.emitChange(); this.userInput = null; } }, // 前往多少頁 render (h) { return ( <span class="el-pagination__jump"> {this.t('el.pagination.goto')} <el-input class="el-pagination__editor is-in-pagination" min={1} max={this.$parent.internalPageCount} value={this.userInput !== null ? this.userInput : this.$parent.internalCurrentPage} type="number" disabled={this.$parent.disabled} nativeOnKeyup={this.handleKeyup} onInput={this.handleInput} onChange={this.handleChange} /> {this.t('el.pagination.pageClassifier')} </span> ); } }, //總共的頁數,組件 Total: { mixins: [Locale], render (h) { return ( typeof this.$parent.total === 'number' ? <span class="el-pagination__total">{this.t('el.pagination.total', { total: this.$parent.total })}</span> : '' ); } }, Pager }, methods: { handleCurrentChange (val) { this.internalCurrentPage = this.getValidCurrentPage(val); this.userChangePageSize = true; // /觸發父組件current-change事件,並傳遞相應的值 this.emitChange(); }, prev () { if (this.disabled) return; const newVal = this.internalCurrentPage - 1; this.internalCurrentPage = this.getValidCurrentPage(newVal); // 用戶點擊上一頁按鈕改變當前頁后觸發 當前頁 //觸發父組件的prev-click事件,並將CurrentPage傳遞出去 this.$emit('prev-click', this.internalCurrentPage); //觸發父組件current-change事件,並傳遞相應的值 this.emitChange(); }, next () { if (this.disabled) return; const newVal = this.internalCurrentPage + 1; this.internalCurrentPage = this.getValidCurrentPage(newVal); // 用戶點擊下一頁按鈕改變當前頁后觸發 當前頁 //觸發父組件的next-click事件,並將CurrentPage傳遞出去 this.$emit('next-click', this.internalCurrentPage); this.emitChange(); }, //校驗需要前往的頁碼的值 getValidCurrentPage (value) { value = parseInt(value, 10); const havePageCount = typeof this.internalPageCount === 'number'; let resetValue; if (!havePageCount) { if (isNaN(value) || value < 1) resetValue = 1; } else { // 如果當前頁碼小於1,則取1;如果當前頁碼大於最大頁碼,則取最大頁碼 if (value < 1) { resetValue = 1; } else if (value > this.internalPageCount) { resetValue = this.internalPageCount; } } //如果當前頁碼是非數字,或者為0,則將當前頁碼置為1,並返回 if (resetValue === undefined && isNaN(value)) { resetValue = 1; } else if (resetValue === 0) { resetValue = 1; } return resetValue === undefined ? value : resetValue; }, emitChange () { this.$nextTick(() => { //用戶改變當前PageSize時,觸發父組件的current-change事件 if (this.internalCurrentPage !== this.lastEmittedPage || this.userChangePageSize) { // currentPage 改變時會觸發 當前頁 this.$emit('current-change', this.internalCurrentPage); // 當前頁賦值給上次點擊的頁面 //lastEmittedPage記錄最后傳遞的CurrentPage的值 this.lastEmittedPage = this.internalCurrentPage; this.userChangePageSize = false; } }); } }, computed: { // 總頁碼 internalPageCount () { if (typeof this.total === 'number') { //總頁數 = 總條目數 / 每頁的顯示條數 return Math.max(1, Math.ceil(this.total / this.internalPageSize)); } else if (typeof this.pageCount === 'number') { //總頁數 return Math.max(1, this.pageCount); } return null; } }, watch: { currentPage: { immediate: true, handler (val) { this.internalCurrentPage = this.getValidCurrentPage(val); } }, pageSize: { immediate: true, handler (val) { this.internalPageSize = isNaN(val) ? 10 : val; } }, // internalCurrentPage改變時去觸發父組件中currentPage更新 internalCurrentPage: { immediate: true, handler (newVal) { this.$emit('update:currentPage', newVal); this.lastEmittedPage = -1; } }, // 總頁碼 internalPageCount (newVal) { /* istanbul ignore if */ const oldPage = this.internalCurrentPage; if (newVal > 0 && oldPage === 0) { this.internalCurrentPage = 1; } else if (oldPage > newVal) { this.internalCurrentPage = newVal === 0 ? 1 : newVal; this.userChangePageSize && this.emitChange(); } this.userChangePageSize = false; } } };
src/pagination.vue
<template> <ul @click="onPagerClick" class="el-pager"> <!-- 第一頁 --> <li :class="{ active: currentPage === 1, disabled }" v-if="pageCount > 0" class="number">1</li> <!-- 向右更多 --> <li class="el-icon more btn-quickprev" :class="[quickprevIconClass, { disabled }]" v-if="showPrevMore" @mouseenter="onMouseenter('left')" @mouseleave="quickprevIconClass = 'el-icon-more'"> </li> <!-- 頁碼 --> <li v-for="pager in pagers" :key="pager" :class="{ active: currentPage === pager, disabled }" class="number">{{ pager }}</li> <!-- 向右的更多 --> <li class="el-icon more btn-quicknext" :class="[quicknextIconClass, { disabled }]" v-if="showNextMore" @mouseenter="onMouseenter('right')" @mouseleave="quicknextIconClass = 'el-icon-more'"> </li> <!-- 總頁數 --> <li :class="{ active: currentPage === pageCount, disabled }" class="number" v-if="pageCount > 1">{{ pageCount }}</li> </ul> </template> <script type="text/babel"> export default { name: 'ElPager', props: { // 當前頁數,支持 .sync 修飾符 number — 1 currentPage: Number, // 總頁數,total 和 page-count 設置任意一個就可以達到顯示頁碼的功能;如果要支持 page-sizes 的更改,則需要使用 total 屬性 Number — — pageCount: Number, // 頁碼按鈕的數量,當總頁數超過該值時會折疊 number 大於等於 5 且小於等於 21 的奇數 7 pagerCount: Number, // 是否禁用 disabled: Boolean }, watch: { // 是否顯示<< showPrevMore(val) { if (!val) this.quickprevIconClass = 'el-icon-more'; }, // 是否顯示>> showNextMore(val) { if (!val) this.quicknextIconClass = 'el-icon-more'; } }, methods: { onPagerClick(event) { const target = event.target; if (target.tagName === 'UL' || this.disabled) { return; } // 找到點擊對象的內容 let newPage = Number(event.target.textContent); const pageCount = this.pageCount;//共顯示多少最大頁碼按鈕數 const currentPage = this.currentPage;// 當前頁 const pagerCountOffset = this.pagerCount - 2; //每次移動的距離 // 點擊... if (target.className.indexOf('more') !== -1) { // 點擊<< if (target.className.indexOf('quickprev') !== -1) { newPage = currentPage - pagerCountOffset; // 點擊>> } else if (target.className.indexOf('quicknext') !== -1) { newPage = currentPage + pagerCountOffset; } } /* istanbul ignore if */ if (!isNaN(newPage)) { // 最小為1 if (newPage < 1) { newPage = 1; } // 最大為pageCount if (newPage > pageCount) { newPage = pageCount; } } // 新舊值不相等 if (newPage !== currentPage) { this.$emit('change', newPage); } }, // 鼠標移入 onMouseenter(direction) { if (this.disabled) return; if (direction === 'left') { // 設置◀️類名 this.quickprevIconClass = 'el-icon-d-arrow-left'; } else { // 設置向右類名 this.quicknextIconClass = 'el-icon-d-arrow-right'; } } }, computed: { pagers() { const pagerCount = this.pagerCount;//展示的最大頁數 // 展示的最大頁數的一半 const halfPagerCount = (pagerCount - 1) / 2; // 當前頁數 const currentPage = Number(this.currentPage); // 總頁數 const pageCount = Number(this.pageCount); let showPrevMore = false; let showNextMore = false; // 總頁數大於設置顯示的最大總頁數 if (pageCount > pagerCount) { // 如果當前頁數大於最大總頁數減去最大總頁數一半 if (currentPage > pagerCount - halfPagerCount) { // 展示向左更多 showPrevMore = true; } // 反之向右更多 if (currentPage < pageCount - halfPagerCount) { showNextMore = true; } } const array = []; // 出現向左更多 if (showPrevMore && !showNextMore) { const startPage = pageCount - (pagerCount - 2); for (let i = startPage; i < pageCount; i++) { array.push(i); } } else if (!showPrevMore && showNextMore) { for (let i = 2; i < pagerCount; i++) { array.push(i); } } else if (showPrevMore && showNextMore) { const offset = Math.floor(pagerCount / 2) - 1; for (let i = currentPage - offset ; i <= currentPage + offset; i++) { array.push(i); } } else { for (let i = 2; i < pageCount; i++) { array.push(i); } } this.showPrevMore = showPrevMore; this.showNextMore = showNextMore; return array; } }, data() { return { current: null, showPrevMore: false, showNextMore: false, quicknextIconClass: 'el-icon-more', quickprevIconClass: 'el-icon-more' }; } }; </script>