vue中的自定義分頁插件組件

介紹一下,已經有不少的vue分頁的組件了,你們都是大同小易,那麼我就結合自身的使用,寫出了一片文章css

clipboard.png

clipboard.png

  • 首先在新建一個分頁模塊

clipboard.png

  1. 在模塊中引入相應的代碼,(內有詳細的註釋)
  2. template中vue

    <div class="page-bar">
       <ul>
           <li class="first">
               <span>共{{dataNum}}條記錄 第 {{cur}} / {{all}} 頁</span>
           </li>
           <li v-if="cur>1">
               <a v-on:click="cur--,pageClick()"><</a>//點擊上一頁
           </li>
           <li v-if="cur==1">
               <a class="banclick"><</a>//點擊第一頁時顯示
           </li>
           <li  class="li_a" v-for="index in indexs" v-bind:class="{ 'active': cur == index}">
               <a v-on:click="btnClick(index)">{{ index }}</a>//頁碼
           </li>
           <li  v-if="cur!=all">
               <a v-on:click="cur++,pageClick()">></a>//點擊下一頁
           </li>
           <li  v-if="cur == all">
               <a class="banclick">></a> //點擊最後一頁時顯示
           </li>
           <li class="last_li">
               <span>共<i>{{all}}</i>頁</span> // 共有多少頁
           </li>
       </ul>
    </div>
  3. style中的內容npm

    .page-bar {
       text-align: center;
       width: 100%;
       height: 36px;
       margin: 0 auto;
       position: relative;
     }
    
     .page-bar ul {
       min-width: 700px;
       display: block;
       overflow: hidden;
       position: absolute;
       top: 50%;
       left: 50%;
       transform: translate(-50%,-50%);
       }
    
    .page-bar li {
       display: block;
       width: 36px;
       height: 36px;
       border-radius: 4px;
       list-style: none;
       overflow: hidden;
       position: relative;
       float: left;
       margin-left: 8px;
    }
    .page-bar .first{
       display: block;
       width: 170px;
       height: 36px;
       font-size: 14px;
       line-height: 36px;
       text-align: center;
    }
    .page-bar .last_li{
       width: 85px;
       height: 36px;
       border: 1px solid #ddd;
    }
    .page-bar .last_li span{
        width: 100%;
       height: 100%;
       line-height: 36px;
       text-align: center;
       float: left;
    }
    .page-bar li:first-child {
       margin-left: 0px
    }
    
    .page-bar a {
       width: 34px;
       height: 34px;
       border: 1px solid #ddd;
       text-decoration: none;
       position: absolute;
       top: 50%;
       left: 50%;
       transform: translate(-50%,-50%);
       /*margin-left: -1px;*/
       line-height:  34px;
       color: #333;
       cursor: pointer
    }
    
    .page-bar .li_a a:hover {
       background-color: #eee;
       border: 1px solid #40A9FF;
       color: #40A9FF;
    }
    
    .page-bar a.banclick {
       cursor: not-allowed;
    }
    
    .page-bar .active a {
       color: #fff;
       cursor: default;
       background-color: #1890FF;
       border-color: #1890FF;
    }
    
    .page-bar i {
       font-style: normal;
       color: #d44950;
       margin: 0px 4px;
       font-size: 14px;
    }
  4. script函數

    export default {
       //顯示的聲明組件
       name: "paging",
      //從父級組件中傳值過來的,你能夠本身設置名字,可是須要跟父級傳入的名字一致!
        props : ["dataAll","dataCur","datanum","dataDatanum"],
       
    
       data() {
           return {
               all: this.dataAll, //總頁數
               cur:  this.dataCur ,//當前頁碼
               num: this.datanum , //一頁顯示的數量  奇數
               dataNum: this.dataDatanum,//數據的數量
               
           }
    
       },
       watch: {
           cur: function(oldValue, newValue) {
           //父組件經過change方法來接受當前的頁碼
               this.$emit('change', oldValue)
              //這裏是直接點擊執行函數
           }
       },
       methods: {
           btnClick: function(data) { //頁碼點擊事件
               if(data != this.cur) {
                   this.cur = data
               }
           },
           pageClick: function() {
               console.log('如今在' + this.cur + '頁');
                //父組件經過change方法來接受當前的頁碼
                 //這裏是點擊下一頁執行函數
               this.$emit('change',  this.cur)
           }
       },
    
       computed: {
           indexs: function() {
               var left = 1;
               var right = this.all;
               var ar = [];
               if(this.all >= this.num ) {
                   if(this.cur > 3 && this.cur < this.all - 2) {
                       left = this.cur -  (this.num-1)/2
                       right = this.cur +  (this.num-1)/2
                   } else {
                       if(this.cur <= 3) {
                           left = 1
                           right =  this.num
                       } else {
                           right = this.all
                           left = this.all - (this.num - 1);
                       }
                   }
               }
               while(left <= right) {
                   ar.push(left)
                   left++
               }
               return ar
           }
    
       }
    
    }
  5. 父級的組件內容this

    <template>
    //這是我本身設置的,能夠根據狀況不用設置不一樣的樣式
           <div class="page">
           //這裏時經過props傳值到子級,並有一個回調change的函數,來獲取本身傳值到父級的值
           <paging :dataAll="all" :dataCur="cur" :datanum="num" :dataDatanum="dataNum" @change="pageChange"></paging>
       </div>
    </template>
    <style scoped>
     .page {
       width: 100%;
       min-width: 1068px;
       height: 36px;
       margin: 40px auto;
     }
    </style>
    <script>
    import Paging from './paging'
    export default {
       name: "homepage",
       components: {
           Paging
       },
       data() {
           return {
               all: 40, //總頁數
               cur: 1, //當前頁碼
               num: 7, //一頁顯示的數量  必須是奇數
               dataNum: 400, //數據的數量
           }
       },
       methods: {
           //子級傳值到父級上來的動態拿去
           pageChange: function(page) {
               this.cur = page
           }
       }
    }
    </script>
  6. 最後從新保存,從新運行spa

    npm run dev

clipboard.png

  1. 注意 1.能夠根據本身喜愛來本身動手作一個分頁,我在其它人的基礎之上添加了頁碼以及當前頁面數,也能夠添加跳轉的頁數(暫時沒有作),也能夠更改css樣式來改變! 2.本人才疏學淺,請你們多多包涵!
相關文章
相關標籤/搜索