当前位置:首页 > uni-app

uniapp 分页组件

2026-02-05 20:09:39uni-app

uniapp 分页组件实现方法

自定义分页组件

创建一个自定义分页组件,通常包含页码按钮、上一页/下一页按钮和总页数显示。以下是一个基础实现示例:

<template>
  <view class="pagination">
    <view class="pagination-btn" @click="handlePrev" :class="{ disabled: currentPage === 1 }">
      上一页
    </view>
    <view 
      v-for="page in pages" 
      :key="page" 
      @click="handlePageChange(page)"
      :class="{ active: currentPage === page }"
      class="pagination-item"
    >
      {{ page }}
    </view>
    <view class="pagination-btn" @click="handleNext" :class="{ disabled: currentPage === totalPages }">
      下一页
    </view>
  </view>
</template>

<script>
export default {
  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)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    handlePageChange(page) {
      if (page !== this.currentPage) {
        this.$emit('change', page)
      }
    },
    handlePrev() {
      if (this.currentPage > 1) {
        this.$emit('change', this.currentPage - 1)
      }
    },
    handleNext() {
      if (this.currentPage < this.totalPages) {
        this.$emit('change', this.currentPage + 1)
      }
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 20px 0;
}
.pagination-item, .pagination-btn {
  padding: 5px 10px;
  margin: 0 5px;
  border: 1px solid #ddd;
  cursor: pointer;
}
.pagination-item.active {
  background-color: #007aff;
  color: white;
}
.pagination-btn.disabled {
  color: #ccc;
  cursor: not-allowed;
}
</style>

使用第三方组件库

uniapp生态中有多个UI组件库提供分页组件,可直接使用:

uniapp 分页组件

  1. uView UI 的分页组件:

    <template>
    <u-pagination 
     :total="total" 
     :current="current" 
     @change="pageChange"
    />
    </template>
  2. ColorUI 的分页组件:

    uniapp 分页组件

    <template>
    <c-pagination 
     :total="total" 
     :current="current" 
     @change="handlePageChange"
    />
    </template>

后端数据分页处理

实现分页功能时,通常需要与后端API配合:

async loadData(page = 1) {
  const res = await uni.request({
    url: 'https://api.example.com/data',
    data: {
      page,
      pageSize: 10
    }
  })
  this.list = res.data.list
  this.total = res.data.total
}

滚动加载更多

对于移动端,可以实现无限滚动加载:

<template>
  <view>
    <view v-for="item in list" :key="item.id">{{ item.name }}</view>
    <view v-if="loading" class="loading">加载中...</view>
    <view v-if="noMore" class="no-more">没有更多了</view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      loading: false,
      noMore: false
    }
  },
  onLoad() {
    this.loadData()
    uni.$on('scrollToBottom', this.loadMore)
  },
  methods: {
    async loadData() {
      this.loading = true
      const res = await uni.request({
        url: 'https://api.example.com/data',
        data: {
          page: this.page,
          pageSize: 10
        }
      })
      this.list = [...this.list, ...res.data.list]
      this.loading = false
      if (res.data.list.length < 10) {
        this.noMore = true
      }
    },
    loadMore() {
      if (!this.loading && !this.noMore) {
        this.page++
        this.loadData()
      }
    }
  }
}
</script>

分页组件优化建议

  • 添加页码过多时的省略显示
  • 支持自定义每页显示数量
  • 添加跳转到指定页码功能
  • 优化移动端触摸体验
  • 添加加载状态提示

以上方法可根据实际项目需求选择使用或组合使用,实现适合业务场景的分页功能。

标签: 分页组件
分享给朋友:

相关文章

vue实现后台分页

vue实现后台分页

实现后台分页的方法 在Vue中实现后台分页通常需要结合前端和后端逻辑,后端负责处理数据分页,前端负责展示分页数据和交互。以下是具体实现方法: 后端API设计 后端需要提供支持分页的API接口,通常…

php实现分页功能

php实现分页功能

PHP实现分页功能的方法 数据库查询与分页逻辑 使用SQL的LIMIT和OFFSET实现分页。假设每页显示10条数据,当前页码为$page: $perPage = 10; $page = isset…

uniapp倒计时组件

uniapp倒计时组件

uniapp倒计时组件实现方法 使用内置组件实现 uniapp提供了<countdown>组件用于倒计时功能,支持自定义格式和样式。示例代码如下: <countdown :…

h5实现左右分页

h5实现左右分页

实现左右分页的HTML5方法 在HTML5中实现左右分页效果可以通过多种方式完成,以下是几种常见的方法: 使用CSS Flexbox布局 Flexbox是一种现代的布局方式,可以轻松实现左右分页效果…

vue实现分页条数

vue实现分页条数

Vue 实现分页条数控制 在 Vue 中实现分页条数控制通常需要结合分页组件和后端 API 支持。以下是几种常见的实现方式: 使用 Element UI 的分页组件 Element UI 提供了强…

uniapp 滑动组件

uniapp 滑动组件

uniapp 滑动组件实现方法 使用swiper组件 uniapp内置的swiper组件可以实现滑动效果,适用于轮播图或页面切换场景。 示例代码: <swiper :indicator-…