Vue 分页器组件

<template>
  <div class="pagination">
    <button>上一页</button>
    <button @click="changecurentpage(1)" v-if="startEnd.start > 1">1</button>
    <button v-if="startEnd.start > 2">···</button>

    <button @click="changecurentpage(startEnd.start+index)" v-for="(item,index) in continuPage" :key="index">{{startEnd.start+index}}</button>


    <button v-if="startEnd.end < pagesTotal -1">···</button>
    <button @click="changecurentpage(pagesTotal)" v-if="startEnd.end < pagesTotal">{{pagesTotal}}</button>
    <button>下一页</button>

    <button style="margin-left: 30px">{{currentPageNo}}/{{pagesTotal}}页</button>
  </div>
</template>

<script>
export default {
  props: ['currentPageno','pageSize','total','continuPage'],
  data() {
    return {
      currentPageNo : this.currentPageno
    }
  },
  methods: {
    changecurentpage(currentPageNo) {
      // if(this.startEnd.end)
      this.currentPageNo = currentPageNo
    }
  },
  computed: {
    pagesTotal() {
      return Math.ceil(this.total/this.pageSize)
    },
    startEnd() {
      let {currentPageNo,continuPage} = this
      let start = currentPageNo-parseInt(continuPage/2)
      if (start < 1) start = 1
      let end = currentPageNo+parseInt(continuPage/2)
      if (end > this.pagesTotal) {
        end = this.pagesTotal
        start = end - continuPage +1
      }
      return {start, end}
    }
  }
};
</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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Vue 分页组件示例: ``` <template> <div class="pagination"> <button v-if="currentPage !== 1" @click="currentPage--" > 上一页 </button> <button v-for="page in pagesToShow" :key="page" :class="{ active: page === currentPage }" @click="currentPage = page" > {{ page }} </button> <button v-if="currentPage !== totalPages" @click="currentPage++" > 下一页 </button> </div> </template> <script> export default { props: { totalPages: { type: Number, required: true }, currentPage: { type: Number, default: 1 }, pagesToShow: { type: Number, default: 5 } }, computed: { pages() { const pagesArray = []; for (let i = 1; i <= this.totalPages; i++) { pagesArray.push(i); } return pagesArray; }, pagesInRange() { const startPage = this.currentPage - Math.floor(this.pagesToShow / 2); const endPage = this.currentPage + Math.floor(this.pagesToShow / 2); return this.pages.filter(page => page >= startPage && page <= endPage); } } }; </script> <style> .pagination { display: flex; justify-content: center; align-items: center; } button { margin: 0 5px; padding: 5px 10px; background-color: #fff; border: 1px solid #ccc; border-radius: 3px; cursor: pointer; } button.active { background-color: #007bff; color: #fff; border-color: #007bff; } </style> ``` 在这个示例中,我们定义了一个名为 `pagination` 的组件,它接受三个 props:`totalPages`,`currentPage` 和 `pagesToShow`。`totalPages` 表示总页数,`currentPage` 表示当前页,`pagesToShow` 表示要显示的页码数量(默认为 5)。 在模板中,我们使用 `v-if` 和 `v-for` 指令来显示上一页、下一页和页码按钮。我们使用 `@click` 监听来响应按钮的点击事件,并将 `currentPage` 设置为相应的页码。 在计算属性中,我们使用 `pages` 数组来生成所有页码。然后,我们使用 `pagesInRange` 计算属性来生成应该显示的页码范围。我们使用当前页和 `pagesToShow` 计算出范围的起始和结束页码,并使用 `filter` 方法从 `pages` 数组中选择应该显示的页码。 最后,我们定义了一些 CSS 样式来使分页看起来更漂亮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值