Vite + vue3 自定义分页器Pagination组件

效果:

在这里插入图片描述

环境:

Windows10

vite: 2.8.0

vue3: 3.2.25

组件Pagination:

html基本构成如下(省略样式),后面再添加功能

从左到右分7个区域:

【上一页】按钮

【1】按钮

【…】省略号

【5 6 7 8 9】中间连续数字

【…】省略号

【n】最后一页按钮

【下一页】按钮

<template>
<div>
  <!-- 上一页 -->
  <button>上一页</button>
  <!-- 第一页 -->
  <button>1</button>
  <!-- 省略号 -->
  <span>...</span>
  <!-- 中间连续页码 -->
  <button>5</button>
  <button>6</button>
  <button>7</button>
  <button>8</button>
  <button>9</button>
  <!-- 省略号 -->
  <span>...</span>
  <!-- 最后一页 -->
  <button >n</button>
  <!-- 下一页 -->
  <button>下一页</button>
</div>
</template>

props

使用defineProps定义,三个参数

  1. totalItems,总的数量,比如数据库查询到的总的商品数量
  2. pageItems,每一页展示的数量
  3. continues,页码中间的连续数字的个数。(如上面的5 6 7 8 9,共5个数字。一般是奇数个,为了对称性)
const props = defineProps({
  // 总数量
  totalItems: Number,
  // 每页的数量
  pageItems: Number,
  // 中间连续页码
  continues: {
    type: Number,
    default: 5
  }
})

data

通过computed,props计算总共多少页。

初始化当前页码是第一页。

// 总页数
const totalPages = computed(() => Math.ceil(props.totalItems / props.pageItems))

// 当前页码
let currentNo = ref((totalPages.value > 0) ? 1 : 0)

最关键的,确定是否显示省略号?

通过计算中间连续部分的起始和终止位置:

  1. 总页数不够,那么肯定不用显示省略号
  2. 总页数够
    1. 判断起始位置,是否太靠近【页码1】按钮
    2. 判断终止位置,是否太靠近【页码n】按钮
// 计算中间连续页码的开始、结束位置
let startEnd = computed(() => {
  let start = 0, end = 0
  // 总页数不够,那么就不存在中间连续页码的问题了
  if(totalPages.value < props.continues) {
    start = 1
    end = totalPages.value
  } else {
    // 连续页码数量的一半
    const dx = Math.floor(props.continues / 2)
    start = currentNo.value - dx
    end = currentNo.value + dx
    // 判断边界(假如有20页)
    if(start < 1) {
      start = 1
      // 1 2 3 4 5 ... 20
      end = props.continues
    }
    if(end > totalPages.value) {
      end = totalPages.value
      // 1 ... 16 17 18 19 20 
      start = totalPages.value - props.continues + 1
    }
  }
  return {start, end}
})

点击跳转

改当前页码就行

/**
 * 跳转页面
 */
function go(e) {
  const target = e.target || e.srcElement
  if(target.nodeName.toLowerCase() === 'button') {
    const goIndex = target.getAttribute('go')
    // 上一页、下一页
    if(['-1', '+1'].includes(goIndex)) {
      currentNo.value = goIndex - 0 + currentNo.value
      if(currentNo.value < 1) currentNo.value = 1
      if(currentNo.value > totalPages.value) currentNo.value = totalPages.value
    } else {
      currentNo.value = goIndex - '0'
    }
  }
}

源码

Pagination.vue

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分页是一个常见的 UI 控件,用于展示数据的分页情况,并提供翻页操作。在 Vue 中,我们可以通过自定义组件的方式来实现分页。下面是一个简单的分页组件示例: ```vue <template> <div class="pagination"> <button :disabled="currentPage === 1" @click="prevPage">上一页</button> <span class="page-num">{{ currentPage }} / {{ totalPages }}</span> <button :disabled="currentPage === totalPages" @click="nextPage">下一页</button> </div> </template> <script> export default { props: { currentPage: { type: Number, required: true }, totalPages: { type: Number, required: true } }, methods: { prevPage() { if (this.currentPage > 1) { this.$emit('page-change', this.currentPage - 1); } }, nextPage() { if (this.currentPage < this.totalPages) { this.$emit('page-change', this.currentPage + 1); } } } } </script> <style scoped> .pagination { display: flex; justify-content: center; align-items: center; margin-top: 20px; } button { margin: 0 10px; padding: 5px 10px; border-radius: 5px; border: none; background-color: #007aff; color: #fff; cursor: pointer; } button:disabled { opacity: 0.5; cursor: not-allowed; } .page-num { font-size: 16px; font-weight: bold; margin: 0 10px; } </style> ``` 在这个示例中,我们定义了两个 props:currentPage 和 totalPages,分别表示当前页和总页数。组件中包含上一页、下一页按钮和页码信息。通过点击按钮来触发 page-change 事件,从而更新父组件中的 currentPage 值,实现翻页操作。 使用该组件时,只需在父组件中传入 currentPage 和 totalPages 值,并监听 page-change 事件即可: ```vue <template> <div> <div v-for="item in items">{{ item }}</div> <pagination :current-page="currentPage" :total-pages="totalPages" @page-change="handlePageChange" /> </div> </template> <script> import Pagination from './Pagination.vue'; export default { components: { Pagination }, data() { return { items: ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'], currentPage: 1, totalPages: 2 } }, methods: { handlePageChange(page) { this.currentPage = page; } } } </script> ``` 这里只是一个简单的分页示例,实际开发中还可以根据需求进行扩展,例如添加页码输入框、每页显示条数控制等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值