Vue3.0 手写分页组件

 pagination.vue:(下面代码可以直接用)

<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>

父组件中接收事件:

<Pagination @current-change="pageChange" :total="total"/>

  const pageChange = page => {
      reqParams.page = page
    }

根据你项目的 要求,每页多少条, 当前第几页,按钮个数..............................

 total    //总条数

pageSize   // 每页多少条

currentPage   //当前第几页

btnCount    // 按钮个数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值