Pagination 组件 【分页器】

分页器已知前提(4个条件/父组件传值):

pageNo当前页,pageSize每页需展示多少条,total 总条数,continues分页器连续页码数

一、创建组件,在components下创建(公用组件)

<template>
  <div class="pagination">
    <button :disabled="pageNo==1" @click="changePage(pageNo-1)" >上一页</button>
    <button v-if="pageNo>3" @click="changePage(1)" :class="{active:pageNo==1}">1</button>
    <button v-if="pageNo>3">···</button>

    <button v-for="(page,index) in startNumAndEndNum.end" :key="index" 
    v-show="startNumAndEndNum.start<=page" @click="changePage(page)"
    :class="{active:pageNo==page}">{{ page }}</button>
  

    <button v-show="pageNo<=(totalPage-continues/2)">···</button>
    <button v-show="pageNo<=(totalPage-continues/2)" 
    @click="changePage(totalPage)" :class="{active:pageNo==totalPage}">{{ totalPage }}</button>
    <button :disabled="pageNo>=totalPage" @click="changePage(page+1)">下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>
  
  <script>
export default {
  name: "Pagination",
  props: ["pageNo", "pageSize", "total", "continues"],
  computed: {
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    startNumAndEndNum() {
      const { pageNo, totalPage, continues } = this;
      let start = 0;
      let end = 0;
      if (continues > totalPage) {
        start = 1;
        end = totalPage;
      } else {
        let leftNum = pageNo - parseInt(continues / 2);
        let rightNum = pageNo + parseInt(continues / 2);
        if(leftNum<=1){start=1;end=continues}
        else if(rightNum>=totalPage){start=totalPage-continues;end=totalPage}
        else{start=leftNum,end=rightNum}
        
        return { start, end };
      }
    },
  },
  methods:{
    changePage(page){this.$emit('getPageNo',page)}
  }

};
</script>
  
  <style lang="scss" scoped>
.pagination {
  text-align: center;
  margin: 40px 0;
  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>

二、在main.js中全局注册

//引用
import Pagination from '@/components/Pagination'

//全局注册
Vue.component(Pagination.name,Pagination)

三、父组件中,vc 实例化 并传值

<!-- 分页器 -->
<Pagination
    :pageNo="searchParmas.pageNo"
    :pageSize="searchParmas.pageSize"
    :total="total"
    :continues="5" 
    @getPageNo="getPageNo"></Pagination>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简易的分页组件实现: ```html <template> <div class="pagination"> <button :disabled="currentPage === 1" @click="changePage(1)">首页</button> <button :disabled="currentPage === 1" @click="changePage(currentPage - 1)">上一页</button> <span v-for="page in pages" :key="page" :class="{ active: currentPage === page }" @click="changePage(page)">{{ page }}</span> <button :disabled="currentPage === totalPage" @click="changePage(currentPage + 1)">下一页</button> <button :disabled="currentPage === totalPage" @click="changePage(totalPage)">末页</button> </div> </template> <script> export default { name: "Pagination", props: { currentPage: { type: Number, required: true }, totalPage: { type: Number, required: true } }, computed: { pages() { let start, end; if (this.totalPage <= 5) { start = 1; end = this.totalPage; } else { if (this.currentPage <= 3) { start = 1; end = 5; } else if (this.currentPage >= this.totalPage - 2) { start = this.totalPage - 4; end = this.totalPage; } else { start = this.currentPage - 2; end = this.currentPage + 2; } } return Array.from({ length: end - start + 1 }, (_, i) => start + i); } }, methods: { changePage(page) { if (page !== this.currentPage) { this.$emit("change", page); } } } }; </script> <style> .pagination { display: flex; justify-content: center; align-items: center; margin: 20px 0; } button, span { margin: 0 5px; padding: 5px 10px; border: none; border-radius: 5px; cursor: pointer; } button[disabled], span.active { background-color: #ccc; cursor: default; } </style> ``` 使用示例: ```vue <template> <div> <div v-for="item in items" :key="item.id">{{ item.name }}</div> <pagination :current-page="currentPage" :total-page="totalPage" @change="handlePageChange" /> </div> </template> <script> import Pagination from "@/components/Pagination.vue"; export default { name: "ItemList", components: { Pagination }, data() { return { items: [], currentPage: 1, totalPage: 1 }; }, mounted() { this.fetchData(); }, methods: { fetchData() { // 发送请求获取数据并更新items、totalPage等数据 }, handlePageChange(page) { this.currentPage = page; this.fetchData(); } } }; </script> ``` 这个分页组件实现了基本的分页功能,包括首页、上一页、下一页、末页和页码列表。其中,页码列表根据当前页码和总页数动态生成,最多显示5个页码,当前页码高亮显示。组件通过 `props` 接收当前页码和总页数,通过 `$emit` 事件传递页码变化的消息。在使用时,只需要在父组件中监听 `change` 事件,更新当前页码并重新获取数据即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值