使用vue脚手架,写一个原生的分页器

参数说明

pageNo:当前是第几页
pageSize:一页展示多少条数据
total 一共有多少条数据
continues :代表连续分页码是多少,一般是5或者7
<template>
  <div class="pagination">
    <button @click="previousPage">上一页</button>
    <button
      :class="{ cur: pageNo === 1 }"
      @click="changePage(1)"
      v-if="startNumAndEndNum.startNum != 1"
    >
      1
    </button>

    <button
      v-if="startNumAndEndNum.startNum != 1 && startNumAndEndNum.startNum != 2"
    >
      ···
    </button>

    <!-- 中间部分 -->

    <button
      v-for="(page, index) in startNumAndEndNum.endNum"
      :key="index"
      v-if="page >= startNumAndEndNum.startNum"
      :class="{ cur: pageNo === page }"
      @click="changePage(page)"
    >
      {{ page }}
    </button>

    <button
      v-if="
        startNumAndEndNum.endNum != totalPage &&
        startNumAndEndNum.endNum != totalPage - 1
      "
    >
      ···
    </button>
    <button
      @click="changePage(totalPage)"
      v-if="startNumAndEndNum.endNum != totalPage"
    >
      {{ totalPage }}
    </button>
    <button @click="nextPage">下一页</button>

    <button style="margin-left: 30px">{{ totalPage }}</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      continues: 5,
      pageSize:10,
      pageNo:1,
      total:91,
      
    };
  },
  //props: ["pageSize", "pageNo", "total"], // 这是从父组件传递过来的参数
  computed: {
    // 计算总共有多少页
    totalPage() {
      // 向上取整
      return Math.ceil(this.total / this.pageSize);
    },

    // 计算连续页码的起始数据以及结束数据
    startNumAndEndNum() {
      let startNum = 0;
      let endNum = 0;
      // 如果总页码数小于continue
      if (this.totalPage <= this.continue) {
        startNum = 1;
        endNum = this.totalPage;
      } else {
        startNum = this.pageNo - parseInt(this.continue / 2);
        endNum = this.pageNo + parseInt(this.continue / 2);
        if (startNum < 1) {
          startNum = 1;
          endNum = this.continue;
        }
        if (this.totalPage < endNum) {
          endNum = this.totalPage;
          startNum = this.totalPage - this.continue + 1;
        }
      }
      return { startNum, endNum };
    },
  },
  methods: {
    changePage(page) {
      // this.$emit("changeCurrentPage", page); //发送给父组件
      this.pageNo=page
    },
    previousPage() {
      if (this.pageNo != 1) {
       // this.$emit("previousPage");
       this.pageNo -=1;
      }
    },
    nextPage() {
      if (this.pageNo != this.totalPage) {
        //this.$emit("nextPage");
        this.pageNo +=1
      }
    },
  },
};
</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;
    }
  }
  .cur {
    background-color: #e1251b;
    color: #fff;
  }
}
</style>

样式:
提示可以自己该样式哦

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值