vue實現(xiàn)分頁組件

vue 實現(xiàn)分頁組件

寫了個后臺管理項目有很多表格的數(shù)據(jù)加匈,寫了個分頁組件


QQ截圖20190315171433.png
  1. 首先新建一個子組件paging.vue,代碼如下:


<template>
  <div>
    <div class="page-helper" v-if="showPageHelper">
      <div class="page-list">
        <!--totalCount/12根據(jù)自己每頁顯示的條數(shù) -->
        <span class="total-content">共{{Math.ceil(totalCount/12)}}頁</span>
      
      
        <span class="page-item" :class="{'disabled': currentPage == 1}"  @click="prevPage">上一頁</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-item" :class="{'disabled': currentPage == totalPage}" @click="nextPage">下一頁</span>
        <span class="ml20 jump-to">跳至</span>
        <input class="page-jump-to input" type="type" v-model="jumpPageNumber">
        <span class="page-style">頁</span>
        <span class="page-item jump-go-btn" @click="goPage(jumpPageNumber)" >確定</span>
      </div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'pageHelper',
  data () {
    return {
      ddd:this.pageNumber,
      currentPage: this.pageNumber,
      currentSize: this.pageSizeArray[0],
      jumpPageNumber: 1,
      showPrevMore: false,
      showNextMore: false
    }
  },
//   獲取父組件傳入的數(shù)據(jù)
  props: {
    pageNumber: {   //當前頁面
      type: Number,
      default: 1
    },
    pageSizeArray: {   //每頁顯示數(shù)量
      type: Array,//Array
      default () {
        return [1]
      }
    },
    totalCount: {   //總條數(shù)
      type: Number,
      default: 150
    },
    pageGroup: {   //連續(xù)頁碼個數(shù)
      type: Number,
      default: 5
    }
  },
  computed: {
    showPageHelper () {
      return this.totalCount > 0
    },
    totalPage () {   //總頁數(shù)
      return Math.ceil(this.totalCount / this.currentSize);
    },
    groupList () {  //獲取分頁碼
      const groupArray = []
      const totalPage = this.totalPage//總頁碼
      const pageGroup = this.pageGroup//顯示每頁個數(shù)
      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
      }
      if(isNaN(jumpPageNumber)){
        jumpPageNumber = 1
        alert("請輸入正確的頁碼")
      }
    let re = /^[1-9]+[0-9]*]*$/
   if (!re.test(jumpPageNumber))
     {
        alert("請輸入正確的頁碼");
        jumpPageNumber = 1
     }

      this.jumpPage(Number(jumpPageNumber))
    },
    jumpPage (pageNumber) {
      if (this.currentPage !== pageNumber) {  //點擊的頁碼不是當前頁碼
        this.currentPage = pageNumber
      //父組件通過change方法來接受當前的頁碼
      // jumpPage
      this.$emit('pagechange', {currentPage: this.currentPage, currentSize: this.currentSize})
      }
    }
  },
  watch: {
    currentSize (newValue, oldValue) {
      if(newValue !== oldValue){
        if(this.currentPage >= this.totalPage){ //當前頁面大于總頁面數(shù)
          this.currentPage = this.totalPage
        }
        this.$emit('jumpPage', {currentPage: this.currentPage, currentSize: this.currentSize})
      }
    }
  }
}
</script>
 
<style scoped>

  .page-helper {
    margin-top:50px;
    font-weight: 500;
    height: 40px;
    text-align: center;
    color: #888;
    margin: 10px auto;
  }
  .page-list {
    font-size: 0;
    height: 50px;
    line-height: 50px;
    /* margin-top:57px; */
  }
  .page-list span {
      /* display: block; */
    font-size: 18px;
    /* margin-right: 5px; */
    user-select: none;
    width: 53px;
    /* height: 40px; */
  }
  .page-list .page-item {
    border: 1px solid #aaa;
    padding: 10px 10px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 5px;
  }
  .page-ellipsis {
    padding: 0 8px;
  }
  .input {
    width: 36px;
    height: 46px;
    font-size: 18px;
    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: 18px;
    height: 50px;
  }
  .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  .disabled{
  
  
    color: #C0C0C0;
    cursor: not-allowed;
   
  }
  .page-list .page-current {
    cursor: default;
    color: #fff;
    background: #D0121B;
    border-color: #D0121B;
  }
  .total-content{
      margin-right:22px;
  }
  .jump-to{
      margin-left:19px;
      margin-right:11px;

  }
  .page-style{
      margin-left:11px;
      margin-right:11px;
  }

</style>

2.在父組件中引入,因為這個項目有搜索功能在頁碼中用到了 loading來更新頁碼數(shù)據(jù)扳剿。

<template>
          <v-pagination @pagechange="pagechange"  :totalCount= total :page-number="pageNumber" :page-size-array="PageSizeArray" v-if="!loading" ></v-pagination>
</template>
<script>
import pagination from '@/components/paging/paging'
export default {
  data(){
return {
     PageSizeArray: [12],     //每頁的條數(shù)
        total:1, //總條數(shù)
        pageNumber: 1,   // 當前的頁數(shù)
       loading:false,//更新頁碼
}
}峦甩,
methods:{
  pagechange:function(page){

        // ajax請求, 向后臺發(fā)送 page.currentPage, 來獲取對應的數(shù)據(jù),
//發(fā)送請求是 loading=true,請求成功后 loading=false,就能更新過頁碼來,如果沒有搜索功能的可以不用添加loading
      },
},
 components: {
    'v-pagination': pagination,
  },
}
</script>

完成分頁組件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末膘盖,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子尽狠,更是在濱河造成了極大的恐慌衔憨,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袄膏,死亡現(xiàn)場離奇詭異践图,居然都是意外死亡,警方通過查閱死者的電腦和手機沉馆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門码党,熙熙樓的掌柜王于貴愁眉苦臉地迎上來德崭,“玉大人,你說我怎么就攤上這事揖盘∶汲” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵兽狭,是天一觀的道長憾股。 經(jīng)常有香客問我,道長箕慧,這世上最難降的妖魔是什么服球? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮颠焦,結(jié)果婚禮上斩熊,老公的妹妹穿的比我還像新娘。我一直安慰自己伐庭,他們只是感情好粉渠,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著圾另,像睡著了一般霸株。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上盯捌,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天淳衙,我揣著相機與錄音,去河邊找鬼饺著。 笑死箫攀,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的幼衰。 我是一名探鬼主播靴跛,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼渡嚣!你這毒婦竟也來了梢睛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤识椰,失蹤者是張志新(化名)和其女友劉穎绝葡,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腹鹉,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡藏畅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了功咒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片愉阎。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡绞蹦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出榜旦,到底是詐尸還是另有隱情幽七,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布溅呢,位于F島的核電站澡屡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏藕届。R本人自食惡果不足惜挪蹭,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一亭饵、第九天 我趴在偏房一處隱蔽的房頂上張望休偶。 院中可真熱鬧,春花似錦辜羊、人聲如沸踏兜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碱妆。三九已至,卻和暖如春昔驱,著一層夾襖步出監(jiān)牢的瞬間疹尾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工骤肛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留纳本,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓腋颠,卻偏偏與公主長得像繁成,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子淑玫,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354