vue3项目封装分页组件

效果

 基础布局

<template>
  <div class="xtx-pagination">
    <a href="javascript:;" class="disabled">上一页</a>
    <span>...</span>
    <a href="javascript:;" class="active">3</a>
    <a href="javascript:;">4</a>
    <a href="javascript:;">5</a>
    <a href="javascript:;">6</a>
    <a href="javascript:;">7</a>
    <span>...</span>
    <a href="javascript:;">下一页</a>
  </div>
</template>
<script>
export default {
  name: 'XtxPagination'
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

完成代码

<template>
  <div class="xtx-pagination">

    <a href="javascript:;" v-if="myCurrentPage==1" class="disabled">上一页</a>
    <a href="javascript:;" v-else @click="go(-1)">上一页</a>

    <span v-if="pageInfo.start>2">...</span>
    <a
      href="javascript:;"
      :class="{active: myCurrentPage === item}"
      v-for="(item,idx) in pageInfo.pager"
      :key="idx"
      @click="changePage(item)"
    >{{item}}</a>

    <span v-if="pageInfo.end < pageInfo.pageCount">...</span>

    <a href="javascript:;" v-if="myCurrentPage==pageInfo.end" class="disabled">下一页</a>
    <a href="javascript:;" v-else @click="go(1)">下一页</a>
  </div>
</template>
<script>
import { computed, ref, watch } from 'vue'
export default {
  name: 'XtxPagination',
  props: {
    total: { type: Number, default: 100 },
    pageSize: { type: Number, default: 10 },
    currentPage: { type: Number, default: 1 },
    btnCount: { type: Number, default: 5 }
  },
  setup (props, { emit }) {
    // 1. myTotal 总条数
    // 2. myPageSize 每页共几条
    // 3. myCurrentPage 当前第几页
    // 4. btnCount 页码按钮的数量
    // 5. pager:  页码数组
    const myTotal = ref(100) // 总条数
    const myPageSize = ref(5) // 每页共几条

    const myCurrentPage = ref(2) // 用户实时点击,修改

    const myBtnCount = ref(5) // 分页按钮的个数5个

    watch(props, () => {
      myTotal.value = props.total
      myPageSize.value = props.pageSize
      myCurrentPage.value = props.currentPage
      myBtnCount.value = props.btnCount
    }, { immediate: true })

    // 让当前的页码处于正中间
    // const pager = ref([1, 2, 3, 4, 5])

    // 根据上边信息,实时计算 pager,起始页码,结束页码
    const pageInfo = computed(() => {
      // 总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value) // 一共有几页

      // 起点
      let start = myCurrentPage.value - Math.floor(myBtnCount.value / 2)
      // 终点
      let end = start + myBtnCount.value - 1

      // 意外1
      if (start < 1) {
        start = 1
        end = myBtnCount.value > pageCount ? pageCount : myBtnCount.value
      }

      // 意外2
      if (end > pageCount) {
        end = pageCount // 最大的页码
        start = (end - myBtnCount.value + 1) < 1 ? 1 : (end - myBtnCount.value + 1)
      }

      const pager = []
      for (let i = start; i <= end; i++) {
        pager.push(i)
      }

      return { pager, start, end, pageCount }
    })

    const changePage = (page) => {
      if (page === myCurrentPage.value) {
        return
      }

      myCurrentPage.value = page
      emit('current-change', page)
    }
    const go = step => {
      myCurrentPage.value = myCurrentPage.value + step
    }
    return { myTotal, myPageSize, myCurrentPage, myBtnCount, pageInfo, changePage, go }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

使用

<XtxPagination @current-change="changePager" :total="total"/>

### 封装 Vue 3 和 TypeScript 的分页组件 为了创建一个功能齐全且易于使用的分页组件,在 Vue 3 中使用 TypeScript 可以为开发人员提供更好的类型安全性和代码提示支持。下面是一个详细的实现方案。 #### 组件结构设计 定义 `Pagination` 组件来处理页面切换逻辑以及样式展示等功能。此组件接收一些属性作为配置项,比如总条目数、每页显示数量等,并通过这些参数计算出总的页码数目[^1]。 #### 属性说明 | 参数名 | 类型 | 默认值 | 描述 | | --- | --- | --- | --- | | totalItems | number | - | 数据总量 | | pageSize | number | 10 | 每一页显示多少条记录 | | currentPage | number | 1 | 当前选中的页码 | #### 实现代码示例 以下是基于上述描述编写的简单版分页器: ```typescript <template> <nav aria-label="Page navigation"> <ul class="pagination justify-content-center"> <!-- 上一页按钮 --> <li :class="{ disabled: currentPage === 1 }" @click="prevPage()"> <a href="#" tabindex="-1">上一页</a> </li> <!-- 数字页码列表 --> <li v-for="(page, index) in pagesList" :key="index" :class="{ active: page === currentPage }" @click="changePage(page)"> <a>{{ page }}</a> </li> <!-- 下一页按钮 --> <li :class="{ disabled: currentPage >= totalPages }" @click="nextPage()"> <a>下一页</a> </li> </ul> </nav> </template> <script setup lang="ts"> import { computed, defineProps, ref } from &#39;vue&#39;; interface Props { totalItems?: number; pageSize?: number; } const props = withDefaults(defineProps<Props>(), { totalItems: 0, pageSize: 10, }); // 计算总共有几页 const totalPages = computed(() => Math.ceil(props.totalItems / props.pageSize)); // 定义当前页状态变量,默认第一页 let currentPage = ref<number>(1); // 动态生成要渲染的页码数组 const pagesList = computed(() => Array.from({ length: totalPages.value }, (_, i) => i + 1) ); function changePage(newPage: number): void { if (newPage !== currentPage.value && newPage > 0 && newPage <= totalPages.value) { currentPage.value = newPage; emit(&#39;update:currentPage&#39;, newPage); } } function prevPage(): void { const nextPage = currentPage.value - 1; changePage(nextPage); } function nextPage(): void { const nextPage = currentPage.value + 1; changePage(nextPage); } </script> ``` 该模板部分展示了带有上下翻页链接和中间具体页码链接的一组导航控件;脚本则负责监听用户的交互操作并更新相应的视图数据[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值