当前位置:首页 > VUE

分页用vue实现

2026-01-15 04:41:07VUE

实现分页的基本思路

在Vue中实现分页功能,通常需要以下几个核心部分:数据列表、当前页码、每页显示数量、总页数计算、页码切换逻辑。以下是一个完整的实现方案。

分页组件代码示例

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

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

<script>
export default {
  data() {
    return {
      dataList: [], // 原始数据
      currentPage: 1, // 当前页码
      itemsPerPage: 10 // 每页显示数量
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.dataList.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.dataList.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    goToPage(page) {
      this.currentPage = page
    }
  },
  created() {
    // 模拟数据获取
    for (let i = 1; i <= 100; i++) {
      this.dataList.push({ id: i, name: `项目 ${i}` })
    }
  }
}
</script>

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

关键实现细节

数据分片计算 通过计算属性paginatedData实现当前页数据的切片显示:

paginatedData() {
  const start = (this.currentPage - 1) * this.itemsPerPage
  const end = start + this.itemsPerPage
  return this.dataList.slice(start, end)
}

总页数计算 基于数据总量和每页显示数量计算总页数:

分页用vue实现

totalPages() {
  return Math.ceil(this.dataList.length / this.itemsPerPage)
}

页码切换控制 提供三个基本方法处理页码切换:

prevPage() {
  if (this.currentPage > 1) {
    this.currentPage--
  }
},
nextPage() {
  if (this.currentPage < this.totalPages) {
    this.currentPage++
  }
},
goToPage(page) {
  this.currentPage = page
}

进阶优化建议

分页组件封装 将分页逻辑抽离为独立组件,通过props接收数据:

分页用vue实现

<pagination 
  :total-items="dataList.length" 
  :items-per-page="itemsPerPage"
  :current-page="currentPage"
  @page-changed="handlePageChange"
/>

异步数据加载 结合API请求实现服务端分页:

async fetchData(page) {
  const response = await axios.get(`/api/items?page=${page}`)
  this.dataList = response.data.items
  this.totalItems = response.data.total
}

显示优化 添加页码范围限制,避免显示过多页码按钮:

visiblePages() {
  const range = 2
  const start = Math.max(1, this.currentPage - range)
  const end = Math.min(this.totalPages, this.currentPage + range)
  return Array.from({ length: end - start + 1 }, (_, i) => start + i)
}

以上实现方案涵盖了Vue分页的核心功能,可根据实际需求进行扩展和调整。

标签: 分页vue
分享给朋友:

相关文章

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现:…