vue基于elemet-ui的Pagination 组件封装分页插件

博客介绍了使用Vue封装分页插件的方法。先新建pagination.js,引入el-pagination、el-select、el-input组件封装全新分页插件,再引入App.vue查看效果,使用时监听页码和每页条数请求接口即可。

1.新建pagination.js 引入el-pagination、el-select、el-input 组件然后封装一个全新的分页插件

<template>
  <div class="pagination" v-if="conf.totalItems > 0">
    <div class="pagination_total">共有 {{ conf.totalItems }} 条记录</div>
    <div class="pagination_conf">
      <el-pagination small layout="prev, pager, next" :current-page="conf.currentPage" :total="conf.totalItems" :page-size="conf.itemsPerPage" @prev-click="prev" @next-click="next" @current-change="changeCurrentPage"></el-pagination>
      <el-select v-model="conf.itemsPerPage" @change="initCurrentPage()">
        <el-option
                v-for="item in itemsPageList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
        </el-option>
      </el-select>
      <div class="jump">
        跳至
        <el-input v-model="jumpPage" @change="jumpTo"></el-input></div>

    </div>

  </div>
</template>

<script>
export default {
  name: 'pagination',
  props: {
    conf: {
      type: Object,
      default(){
        return {
          currentPage: 1,
          itemsPerPage: 10,
          totalItems: 0,
        }
      }
    },
    itemsPageList: {
      type: Array,
      default(){
        return [
          {label:'10条/页',value:10},
          {label:'20条/页',value:20},
          {label:'50条/页',value:50},
          {label:'100条/页',value:100},
        ]
      }
    }
  },
  data(){
    return{
    }
  },
  computed:{
    numberOfPages(){
      return Math.ceil(this.conf.totalItems / this.conf.itemsPerPage)
    },
  },
  created() {
    this.jumpPage = this.conf.currentPage;
  },
  methods:{
    prev(num) {
      if(this.conf.currentPage <= 1){
        return
      } else {
        this.conf.currentPage = num === 'first' ? 1 : num
      }
    },
    next(num) {
      if(this.conf.currentPage >= this.numberOfPages){
        return
      } else {
        this.conf.currentPage = num === 'end' ? 1 : num
      }
    },
    changeCurrentPage(item){
        this.conf.currentPage = item;
        this.jumpPage = item;
    },
    initCurrentPage(){
      this.conf.currentPage = 1;
      this.jumpPage = 1;
    },
    jumpTo(){
      if(Number(this.jumpPage<=0)){
        this.jumpPage = 1;
      }
      let pagecount = Math.ceil(this.conf.totalItems / this.conf.itemsPerPage);
      if(this.jumpPage==null || this.jumpPage<0 ||this.jumpPage>pagecount){
        this.jumpPage = this.conf.currentPage
      }else {
        this.conf.currentPage = this.jumpPage;
        this.changeCurrentPage(this.jumpPage);
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.pagination{
  width: 100%;
  height: 40px;
  .pagination_total{
    width: 160px;
    height: 40px;
    line-height: 40px;
    float: left;
  }
  .pagination_conf{
    width: calc(100% - 160px);
    float: left;
    /deep/ .el-pagination--small{
      margin-top: 9px;
    }
    /deep/ .el-pagination {
      width: 160px;
      float: left;
    }
    /deep/ .el-select{
      width: 100px;
      float: left;
      margin-left: 20px;

    }
    .jump{
      float: left;
    }
    /deep/ .el-input{
      width: 100px;
    }
  }
}
</style>

2.引入App.vue查看效果

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <pagination :conf="listConf"></pagination>
  </div>
</template>

<script>
import pagination from './components/pagination'

export default {
  name: 'App',
  components: {
    pagination
  },
  data(){
    return{
      listConf:{
        currentPage: 1,
        itemsPerPage: 10,
        totalItems: 50,
      }
    }
  },
  watch: {
    'listConf.currentPage':{
      handler:function (newValue, oldValue) {
        if(newValue !== oldValue){
            this.getList();
        }
      }
    },
    'listConf.itemsPerPage':{
      handler:function (newValue, oldValue) {
        if(newValue !== oldValue){
          this.getList();
        }
      }
    },
  },
  methods:{
    getList(){

    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

在这里插入图片描述

3.使用的时候监听页码和每页条数请求接口即可

 watch: {
    'listConf.currentPage':{
      handler:function (newValue, oldValue) {
        if(newValue !== oldValue){
            this.getList();
        }
      }
    },
    'listConf.itemsPerPage':{
      handler:function (newValue, oldValue) {
        if(newValue !== oldValue){
          this.getList();
        }
      }
    },
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值