vue 分页组件开发

本文介绍了如何在Vue.js中开发一个分页组件,详细阐述了分页组件的实现过程和效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分页组件

效果
在这里插入图片描述
代码

<template>
  <el-pagination
    v-bind="$attrs"
    background
    :current-page.sync="page"
    :page-size="pageSize"
    :layout="pageLayout"
    :total="total"
    :pager-count="pagerCount"
    class="base-pagination"
    @current-change="handleCurrentChange"
    @size-change="handleSizeChange"
  >
    <span class="el-pagination__total">
       <label style="padding-right: 5px;">跳至</label>
       <el-input-number  v-model="page"
                         :controls="false"
                         size="mini"
                         :min="1"
                         :max="totalPage" @change="handleCurrentChange">
      </el-input-number>
      <label style="padding-left: 5px;"></label>
    </span>
  </el-pagination>
</template>

<script>
export default {
  props: {
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: Number,
    total: Number,
    layout: {
      type: String,
      default: '->, prev, pager, next, sizes, slot'
    },
    pagerCount: {
      type: Number,
      default: 7
    }
  },
  computed: {
    page: {
      get () {
        return Math.max(this.currentPage, 1)
      },
      set (val) {
        if (val % 1 === 0) {
          this.handleCurrentChange(val)
        }
      }
    },
    totalPage () {
      // 计算总页数
      return Math.ceil(this.total / this.pageSize) || 1
    },
    pageLayout () {
      return this.layout.replace(/jumper/g, 'slot')
    }
  },
  watch: {
    // 每次total变化时都重新计算当前页
    total (val) {
      let currentPage = this.currentPage
      let pageSize = this.pageSize
      let currentFirstIndex = pageSize * (currentPage - 1)
      if (currentFirstIndex >= val) {
        currentPage = Math.ceil(val / pageSize) || 1
      }
      this.change({
        currentPage,
        pageSize
      })
    }
  },
  methods: {
    handleCurrentChange (currentPage) {
      this.change({ currentPage })
    },
    handleSizeChange (pageSize) {
      this.change({ pageSize })
    },
    change ({ currentPage, pageSize }) {
      let change = false
      currentPage = Math.max(currentPage || this.currentPage, 1)
      pageSize = pageSize || this.pageSize
      if (currentPage !== this.currentPage || pageSize !== this.pageSize) {
        change = true
      }
      if (change) {
        this.$emit('change', {
          currentPage,
          pageSize
        })
      }
    }
  }
}
</script>
<style lang="scss">
  @import '@/theme/theme-default.scss';
  @include b(base-pagination) {
    &.el-pagination {
      font-size: 12px;
      .el-input {
        padding: 0px;
      }
      button {
        background-color: $--color-white;
      }
      .btn-next,  .btn-prev,  .el-pager li {
        background-color:  $--color-white;
        border: 1px solid;
      }
      &.is-background .el-pager li:not(.disabled).active {
        background-color: $--color-primary;
      }
      .el-pagination__sizes .el-input .el-input__inner {
        font-size: 12px;
        height: 28px;
        line-height: 28px;
        &:hover {
          color: $--color-primary;
          border-color: $--color-primary;
       }
      }
      .el-pagination__total {
        span {
          font-size: 12px;
        }
      }
      .el-pager li {
        font-size: 12px;
        margin: 0 2px;
        &.number:hover {
           color: $--color-primary;
         }
      }
      .el-pagination__jump {
        margin-left: 10px;
      }
      .el-input-number {
        width: 32px;
        .el-input .el-input__inner {
          text-align: center;
          padding: 0;
          border-radius: 4px;
          height: 28px;
          font-size: 12px;
        }
      }
      .btn-prev {
        padding-left: 4px;
        padding-right: 2px;
        span {
          font-size: 12px;
        }
      }
      .btn-next {
        padding-left: 2px;
        padding-right: 4px;
        span {
          font-size: 12px;
        }
      }
    }
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值