当前位置:首页 > VUE

vue分页功能实现

2026-02-20 13:56:44VUE

Vue 分页功能实现

基础分页组件实现

创建分页组件 Pagination.vue,包含页码按钮、上一页/下一页逻辑:

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="goToPage(page)"
        :class="{ active: currentPage === page }"
      >{{ page }}</button>
    </span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    prevPage() {
      this.$emit('page-changed', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-changed', this.currentPage + 1)
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

在父组件中使用分页

实现数据分页逻辑,监听分页组件事件:

vue分页功能实现

<template>
  <div>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">{{ item.name }}</li>
    </ul>
    <Pagination
      :total-items="items.length"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      items: [], // 从API获取或本地数据
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
    }
  }
}
</script>

高级分页功能扩展

添加页码省略功能,避免过多页码按钮:

vue分页功能实现

// 在Pagination组件的pages计算属性中修改
pages() {
  const current = this.currentPage
  const total = this.totalPages
  const range = []

  // 总是显示第一页
  range.push(1)

  // 当前页前后各显示2页
  for (let i = Math.max(2, current - 2); i <= Math.min(current + 2, total - 1); i++) {
    range.push(i)
  }

  // 总是显示最后一页
  if (total > 1) {
    range.push(total)
  }

  // 添加省略号逻辑
  const filtered = []
  range.forEach((page, i) => {
    if (i > 0 && page - range[i-1] > 1) {
      filtered.push('...')
    }
    filtered.push(page)
  })

  return filtered
}

与API结合的分页实现

当数据来自后端API时,修改获取数据方法:

methods: {
  async fetchData(page = 1) {
    const response = await axios.get('/api/items', {
      params: {
        page,
        per_page: this.itemsPerPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.total_count
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData(page)
  }
},
created() {
  this.fetchData()
}

分页样式优化

添加更美观的样式方案:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination button {
  padding: 5px 10px;
  margin: 0 2px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  border-radius: 3px;
}

.pagination button:hover:not(:disabled) {
  background: #f5f5f5;
}

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

标签: 分页功能
分享给朋友:

相关文章

vue实现收货功能

vue实现收货功能

Vue实现收货功能的基本思路 收货功能通常涉及前端界面交互、后端数据更新和状态管理。在Vue中,可以通过组件化开发实现这一功能,结合API调用与状态管理工具(如Vuex或Pinia)。 创建收货按钮…

vue实现论坛功能

vue实现论坛功能

实现论坛功能的核心模块 论坛功能通常包含用户发帖、回帖、分类浏览、搜索等核心模块。Vue.js 作为前端框架,需配合后端 API 实现数据交互。 基础项目结构搭建 使用 Vue CLI 或 Vite…

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

vue实现注册功能

vue实现注册功能

前端实现注册功能 在Vue中实现注册功能通常需要结合表单验证、HTTP请求和状态管理。以下是一个基于Vue 3和Element Plus的完整实现方案: 模板部分 <template>…

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子来…

vue实现小说分页

vue实现小说分页

实现小说分页功能 Vue.js可以通过计算属性、动态渲染和分页逻辑实现小说分页功能。以下是具体实现方法: 数据准备 需要将小说内容存储为字符串或数组格式,建议按章节拆分: data() { r…