【VUE学习笔记】前端三大件之分页器

前端三大件:轮播图、分页、日历。这仨属于前端开发常见的三种业务

虽然有很多成品分页器可以调用,但是我们还是尽量知道原理,方便实现定制化
直接上代码,这是vue组件

<template>
  <div class="pagination">
    <button :disabled="pageNo == 1" @click="$emit('getPageNo', pageNo - 1)">上一页</button>
    <button :class="{ active: pageNo == 1 }" v-if="startAndEndPage.startPage > 1" @click="$emit('getPageNo', 1)">1</button>
    <button v-if="startAndEndPage.startPage > 2" @click="$emit('getPageNo', 2)">...</button>

    <button
      :class="{ active: pageNo == index + startAndEndPage.startPage - 1 }"
      v-for="index in startAndEndPage.endPage - startAndEndPage.startPage"
      :key="index"
      @click="$emit('getPageNo', startAndEndPage.startPage + index - 1)"
    >
      {{ startAndEndPage.startPage + index - 1 }}
    </button>

    <button v-if="startAndEndPage.endPage < totalPage" @click="$emit('getPageNo', totalPage - 1)">···</button>
    <button :class="{ active: pageNo == totalPage }" @click="$emit('getPageNo', totalPage)">{{ totalPage }}</button>
    <button :disabled="pageNo == totalPage" @click="$emit('getPageNo', pageNo + 1)">下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["total", "pageSize", "pageNo", "continues"],
  computed: {
    // 计算总页数
    totalPage() {
      //Math.ceil(总条数/每页显示条数),向上取整
      return Math.ceil(this.total / this.pageSize);
    },
    // 计算中间页数
    middlePage() {
      // 如果总页数小于等于continues页,则返回总页数;如果总页数大于continues页,则返回continues页
      return this.totalPage <= this.continues ? this.totalPage : this.continues;
    },
    // 计算中间页数的起始页数和结束页数,并以对象形式返回
    startAndEndPage() {
      // 如果总页数小于等于continues页,则返回1和总页数
      if (this.totalPage <= this.continues) {
        return {
          startPage: 1,
          endPage: this.totalPage,
        };
      }
      // 如果总页数大于continues页
      else {
        //如果左边到头,则返回1和往后continues个页
        if (this.pageNo - Math.floor(this.continues / 2) <= 1) {
          return {
            startPage: 1,
            endPage: this.continues + 1,
          };
        }
        //如果右边到头,则返回右边往前continues个页和尾页
        if (this.pageNo + Math.floor(this.continues / 2) >= this.totalPage) {
          return {
            startPage: this.totalPage - this.continues,
            endPage: this.totalPage,
          };
        } else//如果左右都不到头,则正常返回
          return {
            startPage: this.pageNo - Math.floor(this.continues / 2),
            endPage: this.pageNo + Math.floor(this.continues / 2) + 1,
          };
      }
    },
  },
  watch: {
    pageNo(newVal, oldVal) {
      console.log("当前页码是:" + newVal);
    },
  },
  methods: {},
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值