当前位置:首页 > 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 实现组件循环图片的方法 在 Vue 中实现组件循环图片通常可以通过 v-for 指令结合动态数据绑定完成。以下是几种常见的实现方式: 使用静态图片路径数组 假设有一组本地图片路径,可以通过数…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…

jquery 分页

jquery 分页

实现 jQuery 分页的方法 客户端分页实现 使用 jQuery 实现客户端分页的核心思路是通过 JavaScript 控制数据的显示与隐藏。假设有一个包含数据的 HTML 列表或表格: // 分…