当前位置:首页 > 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 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法: 使用 Vue 组件库的分页组件 许多 Vue UI 组件库(如 Element UI、Va…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

vue前端实现分页

vue前端实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。核心逻辑包括计算总页数、处理当前页码变化以及动态加载数据。 使用Element UI的分页组件 Element…

php分页功能的实现

php分页功能的实现

分页功能的基本原理 分页功能的核心是通过SQL的LIMIT子句实现数据分段查询。LIMIT接受两个参数:起始位置偏移量和每页记录数。例如查询第2页(每页10条)的SQL语句为: SELECT * F…

vue实现父子组件

vue实现父子组件

Vue 父子组件通信实现方式 1. 父组件向子组件传递数据(Props) 父组件通过 props 向子组件传递数据,子组件通过 props 接收数据。 父组件模板: <template>…

vue怎样实现分页

vue怎样实现分页

Vue实现分页的方法 使用第三方库(如Element UI或Ant Design Vue) Element UI的Pagination组件可以直接集成到Vue项目中。安装Element UI后,在组件…