封装element的分页组件(el-pagination),可重复使用

element的分页组件:

<template>
  <div class="paginationWrap">
    <el-pagination
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="paginationLayout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
export default {
  name: 'Pagination',
  props: {
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50, 500]
      }
    }, // 每页显示条目个数,页码数据量尺寸
    paginationLayout: { type: String, default: 'slot, total, sizes, prev, pager, next, jumper' }, // 分页组件布局
    total: { type: Number, default: 0 }, // 总条目数,数据量
    page: { type: Number, default: 1 }, // 页码
    background: { type: Boolean, default: true }, // 是否为分页按钮添加背景色
    limit: { type: Number, default: 20 }, // 一页最大显示数
    pageHidden: { type: Boolean, default: false } // 是否显示分页
  },
  data() {
    return {}
  },
  computed: {
    /** 当前页 */
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    /** 页码 */
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    /** 页码改变时会触发 */
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    /** 切换当前页时触发 */
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style scoped>
.paginationWrap {
  margin-top: 30px;
}
/**  分页栏样式 */
.pagination-container {
  padding: 16px 16px;
}
.pagination-container.hidden {
  display: none;
}
/** 分页布局左右分开 */
.el-pagination {
  width: 100%;
  text-align: right;
}
.pagination /deep/ .el-pagination__jump {
  float: right;
}
</style>

调用的页面:
<template>
  <div>
    <Pagination :total="totalCount" :page.sync="page" :limit.sync="listQuery.page.pageSize" @pagination="getDetail" />
  </div>
</template>

<script>
import API from '@/api/base'
import Pagination from '../components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      apiName: 'apiurl',
      content : [],
      listQuery: {
        // 	分页参数
        page: {
          pageSize: 10, // 每页多少条
          pageIndex: 1 // 页码
        },
        query: {}
      },
      page: 1, // 页码
      totalCount: 0 // 表格数据总数
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    // 获取已经报考信息
    async getDetail(val) {
      if (val) {
        this.page = val.page
        if (val.limit) {
          this.listQuery.page.pageIndex = this.page
        } else {
          this.listQuery.page.pageIndex = this.page
        }
      }
      await API.dataPost(this.apiName, this.listQuery, 'pageChange').then((res) => {
        if (res.code === '0000') {
          this.content = res.data.content
          this.totalCount = parseInt(res.data.totalCount) // 数据总数量
        } else {
          this.$notify({
            title: '提示',
            message: res.message,
            duration: 2000,
            type: 'warning'
          })
        }
      })
    }
}
</script>

<style scoped>

</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值