Uniapp封装分页器(类似element分页器)

可直接复制使用,效果图:

<template>
  <view class="pagination">
    <text :disabled="page <= 1" @click="gotoPage(page - 1)"><</text>
    <text v-for="item in pages" :key="getItemKey(item)" :class="{ active: item === page, ellipsis: item === '...' }" @click="gotoPage(item)">{{ item }}</text>
    <text :disabled="page >= totalPages" @click="gotoPage(page + 1)">></text>
  </view>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: { // 总条目数
      type: Number,
      required: true
    },
    pageSize: { // 每页显示的条目数
      type: Number,
      default: 10
    },
    currentPage: { // 当前页码
      type: Number,
      default: 1
    }
  },
  computed: {
    totalPages() { // 总页数
      return Math.ceil(this.total / this.pageSize)
    },
    page() { // 当前页码,限定在 1 和总页数之间
      return Math.max(1, Math.min(this.currentPage, this.totalPages))
    },
    pages() { // 可点击的页码数组,最多显示 5 个页码
      const arr = []
      let start = Math.max(this.page - 2, 1)
      let end = Math.min(start + 4, this.totalPages)
      if (end - start < 4) {
        end = Math.min(start + 4, this.totalPages)
        start = Math.max(end - 4, 1)
      }
      for (let i = start; i <= end; i++) {
        arr.push(i)
      }
      if (end < this.totalPages) {
        arr.push('...', this.totalPages)
      }
      return arr
    }
  },
  methods: {
    getItemKey(item) { // 获取项的 key
      return typeof item === 'number' ? item.toString() : item
    },
    gotoPage(page) { // 跳转到指定页码
      if (page > 0 && page <= this.totalPages) {
        this.$emit('page-change', page)
      }
    }
  }
}
</script>

<style scoped>
.pagination {
  display: flex;
  align-items: center;
  justify-content: right;
  margin-top: 20px;
}

text {
  display: inline-block;
  padding: 4px 6px;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 3px;
  margin-right: 5px;
  color: #2bc993;
}

text.active {
  background-color: #2bc993;
  color: #fff;
}

text.ellipsis {
  cursor: default;
}
</style>
<template>
  <view>
	  <pagination :total="total" :page-size="queryParams.pageSize" :current-page="queryParams.pageNum" @page-change="handlePageChange" />
  </view>
</template>

<script>
import Pagination from '@/components/pagination.vue';
export default {
  components: {
    Pagination
  },
  data() {
    return {
	  total: 550,
	  queryParams:{
		pageSize: 33,
		pageNum: 1,
	  },
    };
  },
  methods: {
	//运行记录分页
	handlePageChange(page) {
	  this.queryParams.pageNum = page;
	},
  },
};
</script>

<style lang="scss" scoped>

</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值