1. 程式人生 > >vue2自定義分頁元件,可設定每頁顯示數量,指定跳轉具體頁面

vue2自定義分頁元件,可設定每頁顯示數量,指定跳轉具體頁面

分頁元件
<template>
  <div>
    <div class="page-helper" v-if="showPageHelper">
      <div class="page-list">
        <span>共{{ totalPage }}頁 / {{ totalCount }}條</span>
        <span class="page-item" @click="jumpPage(1)">首頁</span>
        <span class="page-item" :class="{'disabled': currentPage === 1}"  @click="prevPage">上一頁</span>
        <span class="page-ellipsis" v-if="showPrevMore" @click="showPrevPage">...</span>
        <span class="page-item" v-for="num in groupList" :class="{'page-current':currentPage===num}" :key="num"
              @click="jumpPage(num)">{{num}}</span>
        <span class="page-ellipsis" v-if="showNextMore" @click="showNextPage">...</span>
        <span class="page-item" :class="{'disabled': currentPage === totalPage}" @click="nextPage">下一頁</span>
        <span class="page-item" @click="jumpPage(totalPage)">末頁</span>
        <select class="page-select" @change="jumpPage(currentPage)" v-model="currentSize">
          <option v-for="size in pageSizeArray" :key="size" :value="size">{{ size }}條/頁</option>
        </select>
        <span class="ml20">跳至</span>
        <span class="page-jump-to"><input type="type" v-model="jumpPageNumber"></span>
        <span>頁</span>
        <span class="page-item jump-go-btn" @click="goPage(jumpPageNumber)">GO</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'pageHelper',
  data () {
    return {
      currentPage: this.pageNumber,
      currentSize: this.pageSizeArray[0],
      jumpPageNumber: 1,
      showPrevMore: false,
      showNextMore: false
    }
  },
  props: {
    pageNumber: {   //當前頁面
      type: Number,
      default: 1
    },
    pageSizeArray: {   //每頁顯示數量
      type: Array,
      default () {
        return [10,20,30,50]
      }
    },
    totalCount: {   //總條數
      type: Number,
      default: 1000
    },
    pageGroup: {   //連續頁碼個數
      type: Number,
      default: 5
    }
  },
  computed: {
    showPageHelper () {
      return this.totalCount > 0
    },
    totalPage () {   //總頁數
      return Math.ceil(this.totalCount / this.currentSize);
    },
    groupList () {  //獲取分頁碼
      const groupArray = []
      const totalPage = this.totalPage
      const pageGroup = this.pageGroup
      const _offset = (pageGroup - 1) / 2
      let current = this.currentPage
      const offset = {
        start: current - _offset,
        end: current + _offset
      }
      if (offset.start < 1) {
        offset.end = offset.end + (1 - offset.start)
        offset.start = 1
      }
      if (offset.end > totalPage) {
        offset.start = offset.start - (offset.end - totalPage)
        offset.end = totalPage
      }
      if (offset.start < 1) offset.start = 1
      this.showPrevMore = (offset.start > 1)
      this.showNextMore = (offset.end < totalPage)
      for (let i = offset.start; i <= offset.end; i++) {
        groupArray.push(i)
      }
      return groupArray
    }
  },
  methods: {
    prevPage () {
      if (this.currentPage > 1) {
        this.jumpPage(this.currentPage - 1)
      }
    },
    nextPage () {
      if (this.currentPage < this.totalPage) {
        this.jumpPage(this.currentPage + 1)
      }
    },
    showPrevPage() {
      this.currentPage = this.currentPage - this.pageGroup > 0 ? this.currentPage - this.pageGroup : 1
    },
    showNextPage() {
      this.currentPage = this.currentPage + this.pageGroup < this.totalPage ? this.currentPage + this.pageGroup : this.totalPage
    },
    goPage (jumpPageNumber) {
      if(Number(jumpPageNumber) <= 0){
        jumpPageNumber = 1
      }if(Number(jumpPageNumber) >= this.totalPage){
        jumpPageNumber = this.totalPage
      }
      this.jumpPage(Number(jumpPageNumber))
    },
    jumpPage (pageNumber) {
      if (this.currentPage !== pageNumber) {  //點選的頁碼不是當前頁碼
        this.currentPage = pageNumber
      //父元件通過change方法來接受當前的頁碼
      this.$emit('jumpPage', {currentPage: this.currentPage, currentSize: this.currentSize})
      }
    }
  },
  watch: {
    currentSize (newValue, oldValue) {
      if(newValue !== oldValue){
        if(this.currentPage >= this.totalPage){ //當前頁面大於總頁面數
          this.currentPage = this.totalPage
        }
        this.$emit('jumpPage', {currentPage: this.currentPage, currentSize: this.currentSize})
      }
    }
  }
}
</script>

<style scoped>
  .page-helper {
    font-weight: 500;
    height: 40px;
    text-align: center;
    color: #888;
    margin: 10px auto;
  }
  .page-list {
    font-size: 0;
    height: 50px;
    line-height: 50px;
  }
  .page-list span {
    font-size: 14px;
    margin-right: 5px;
    user-select: none;
  }
  .page-list .page-item {
    border: 1px solid #aaa;
    padding: 5px 8px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 5px;
  }
  .page-ellipsis {
    padding: 0 8px;
  }
  .page-jump-to input {
    width: 45px;
    height: 26px;
    font-size: 13px;
    border: 1px solid #aaa;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    text-align: center;
  }
  .page-list .jump-go-btn {
    font-size: 12px;
  }
  .page-select{
    border: 1px solid #aaa;
    padding: 5px 8px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 5px;
    margin-left: 5px;
  }
  .page-list .page-item .disabled{
    pointer-events: none;
    background: #ddd;
  }
  .page-list .page-current {
    cursor: default;
    color: #fff;
    background: #337ab7;
    border-color: #337ab7;
  }
</style>

父頁面

<page-helper @jumpPage="jumpPage" :page-number="pageNumber" page-size-array="PageSizeArray"></page-helper>

效果圖