国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

一個(gè)可復(fù)用的vue分頁(yè)組件

2019-11-19 16:35:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

不廢話,先上組件文件pages.vue:

<template> <div class="pages-box" v-if="pageTotal > 0">  <ul class="pages">   <li class="pages-prev">    <a v-if="pageNow != 1" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="prevClick">上一頁(yè)</a>   </li>   <!--如果只有一頁(yè)就不顯示固定的第一個(gè)分頁(yè)按鈕了,避免重復(fù)-->   <template v-if="pageTotal > 1">    <li v-for="i in pageBegin" class="pages-li" :class="{active:i == pageNow}">     <span v-if="i == pageNow" v-text="i"></span>     <a v-else href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="pageClick" v-text="i"></a>    </li>   </template>   <li v-if="ellipsis[0] > slider">    <span>...</span>   </li>   <li v-for="i in pageMiddle" class="pages-li" :class="{active:i == pageNow}">    <span v-if="i == pageNow" v-text="i"></span>    <a v-else href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="pageClick" v-text="i"></a>   </li>   <li v-if="pageTotal - ellipsis[1] > slider">    <span>...</span>   </li>   <li v-for="i in pageEnd" class="pages-li" :class="{active:i == pageNow}">    <span v-if="i == pageNow" v-text="i"></span>    <a v-else href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="pageClick" v-text="i"></a>   </li>   <li class="pages-next">    <a v-if="pageNow != pageTotal" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="nextClick">下一頁(yè)</a>   </li>  </ul> </div></template><script> export default{  name: 'pages',  props: {   //總頁(yè)數(shù)   total: {    type: [Number, String],    required: true   },   //當(dāng)前頁(yè)   now: {    type: [Number, String],    default: 1   }  },  data() {   return {    //當(dāng)前頁(yè)    pageNow: this.now,    //總頁(yè)數(shù)    pageTotal: this.total,    //輸入的頁(yè)碼    pageNum: "",    //顯示分頁(yè)按鈕的個(gè)數(shù)    length: 8,    //前后固定的分頁(yè)按鈕個(gè)數(shù)    slider: 1   }  },  watch: {   total(val){    let page_total = parseInt(val);    page_total = (isNaN(page_total) || page_total < 1) ? 1 : page_total;    this.pageTotal = page_total;   },   now(val){    let page_now = parseInt(val);    page_now = (isNaN(page_now) || this.pageTotal < 2 || page_now < 1) ? 1 : page_now;    page_now = page_now > this.pageTotal ? this.pageTotal : page_now;    this.pageNow = page_now;   }  },  computed: {   //前邊顯示固定分頁(yè)數(shù)   pageBegin(){    return Math.min(this.slider, this.ellipsis[0]);   },   //中間顯示分頁(yè)數(shù)   pageMiddle(){    let arr = [];    for (let i = this.ellipsis[0] + 1; i <= this.ellipsis[1]; i++) {     arr.push(i);    }    return arr;   },   //后邊顯示分頁(yè)數(shù)   pageEnd(){    let arr = [];    for (let i = this.ellipsis[2] + 1; i <= this.pageTotal; i++) {     arr.push(i);    }    return arr;   },   /**    * 出現(xiàn)三個(gè)點(diǎn)時(shí)的分頁(yè)的范圍    * @returns {*[]}    * begin: 開(kāi)始頁(yè)碼    * end: 結(jié)束頁(yè)碼    * end_max: 結(jié)束頁(yè)碼的最大值    */   ellipsis() {    let end_max = this.pageTotal - this.slider;    let begin = this.pageNow - (this.length / 2) + this.slider;    begin = begin < 1 ? 1 : begin;    let end = begin + this.length - 2 * this.slider;    //當(dāng)begin達(dá)到最小值后需要根據(jù)begin重新計(jì)算end以保證顯示的分頁(yè)按鈕個(gè)數(shù)不變    end = begin < this.slider ? (end + this.slider - begin) : end;    if (end >= end_max) {     end = end_max;     //當(dāng)end達(dá)到最大值后需要根據(jù)end重新計(jì)算begin以保證顯示的分頁(yè)按鈕個(gè)數(shù)不變     begin = (end - this.length + 2 * this.slider) < 1 ? 1 : (end - this.length + 2 * this.slider);    }    return [begin, end, end_max];   }  },  methods: {   //上一頁(yè)   prevClick() {    this.pageNow--;    this.pageNow = this.pageNow < 1 ? 1 : this.pageNow;    this.changePage(this.pageNow);   },   //下一頁(yè)   nextClick() {    this.pageNow++;    this.pageNow = this.pageNow > this.pageTotal ? this.pageTotal : this.pageNow;    this.changePage(this.pageNow);   },   //點(diǎn)擊頁(yè)碼   pageClick(e) {    this.pageNow = Number(e.target.innerText.trim());    this.changePage(this.pageNow);   },   //輸入頁(yè)碼   pageInput(e){    let num = parseInt(e.target.innerText);    if(isNaN(num)){     this.pageNum = '';     e.target.innerText = '';    } else {     this.pageNum = num;     //e.target.innerText = num;    }   },   //跳轉(zhuǎn)到輸入的頁(yè)碼   goClick() {    this.pageNum = this.pageNum < 1 ? 1 : this.pageNum;    this.pageNum = this.pageNum > this.pageTotal ? this.pageTotal : this.pageNum;    this.pageNow = this.pageNum;    this.pageNum = "";    this.changePage(this.pageNow);   },   // 切換分頁(yè)   changePage(page){    let {name, params, query} = this.$route;    this.$router.push({     name,     params: Object.assign(params, {page}),     query    });   }  } }</script><style lang="sass" type="text/scss" rel="stylesheet/scss"> @import '../scss/base/variables'; .pages-box{  position: relative;  padding: 5px 10px;  margin: 20px 0;  text-align: center; } .pages{  display: inline-block;  padding: 10px 0;  &:after{   content: "";   display: table;   line-height: 0;   clear: both;  }  li{   float: left;   height: 20px;   line-height: 20px;   text-align: center;   margin: 0 2px;   box-sizing: border-box;   font-size: 13px;   span, a{    display: block;    width: 100%;    height: 100%;    padding: 0 2px;    box-sizing: border-box;   }  }  .pages-li{   min-width: 30px;   border: 1px solid $theme;   color: $theme;   a{    color: $theme;   }   &.active{    span{     background: $theme;     color: #fff;    }   }  }  .pages-prev, .pages-next{   padding: 0 8px;   font-size: 12px;   a{    display: block;    height: 100%;    position: relative;    color: $theme;    &:before{     content: '';     position: absolute;     top: 50%;     display: block;     width: 6px;     height: 6px;margin-top:-4px;     border-left: 1px solid $theme;     border-top: 1px solid $theme;    }   }  }  .pages-prev a{   padding-left: 8px;   &:before{    transform:rotate(-45deg);    left: 0;   }  }  .pages-next a{   padding-right: 8px;   &:before{    transform:rotate(135deg);    right: 0;   }  }  .pages-num{   .num-input{    min-width: 20px;    height: 20px;    padding: 0 5px;    line-height: 20px;    border-radius: 2px;    border: 1px solid $theme;    color: $theme;    text-align: center;    outline: none;   }  }  .pages-go{   a{    color: $theme;   }   span{    color: #666;   }  } }</style>

使用方法:

在需要分頁(yè)的地方使用分頁(yè)組件標(biāo)簽,比如這里的order.vue:

<!--分頁(yè)組件--><pages :now="page" :total="totalPage" v-if="totalPage > 0"></pages>

在data中設(shè)置當(dāng)前頁(yè)和總頁(yè)面的默認(rèn)值

data(){    return {      totalPage:1,      page:1,        }    },

考慮一下我們希望我們點(diǎn)擊頁(yè)數(shù)按鈕后發(fā)生什么

首先,點(diǎn)擊某頁(yè)數(shù)時(shí)路由會(huì)改變頁(yè)數(shù),從路由獲取當(dāng)前頁(yè)

this.page = this.$route.params.page;

接著,我們希望有一個(gè)getorderfromServer方法將當(dāng)前頁(yè)數(shù)發(fā)送給服務(wù)器,再將返回的數(shù)據(jù)更新在頁(yè)面上

getorderfromServer({          currentPage:this.page        })

最后調(diào)用的方法:

methods: {      // 查詢?nèi)坑唵?     getorderfromServer(){        this.loading = true;        this.page = this.$route.params.page;        getorderfromServer({          currentPage: this.page,          orderTimeStart:this.orderTimeStart,          orderTimeEnd:this.orderTimeEnd,          serviceName:this.serviceName,          shopName:this.shopName,          status: this.status        }).then(({code, data}) => {          if (code == 200) {            this.Orderlist = data.list;            this.totalPage = data.totalPage;          }          this.loading = false;        }).catch(err => {          this.tip('服務(wù)內(nèi)部錯(cuò)誤', 'error');          this.Orderlist = {};          this.loading = false;        });      },    }

注意通過(guò)路由對(duì)方法作出響應(yīng),每次路由改變都調(diào)用此方法以更新頁(yè)面

watch: {      $route: 'getorderfromServer'    }

還要對(duì)路由信息進(jìn)行改造,讓每一頁(yè)(尤其是第一頁(yè))都有路由頁(yè)數(shù)信息,可以對(duì)第一頁(yè)進(jìn)行重定向以達(dá)到目的:

{  path: 'order',  redirect: 'order/page/1',},{  path: 'order/page/:page',  component(resolve){    require.ensure([], function (require) {      resolve(require('../modules/personal/order/myorder.vue'));    }, 'modules/personal')  },  name:'order',  meta: {    login: 'none'  }},

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新泰市| 旬邑县| 新巴尔虎左旗| 崇信县| 安义县| 邢台县| 神农架林区| 淅川县| 鹤峰县| 新乡县| 齐河县| 合作市| 晋城| 武隆县| 潍坊市| 澎湖县| 开平市| 南投县| 搜索| 齐齐哈尔市| 铁岭市| 拜城县| 司法| 常德市| 井冈山市| 潞西市| 宽甸| 武夷山市| 墨江| 崇文区| 金寨县| 邮箱| 吴川市| 会昌县| 永仁县| 巴林左旗| 白朗县| 东乌珠穆沁旗| 闸北区| 永济市| 岳阳县|