当前位置:首页 > VUE

vue实现简单分页

2026-01-19 04:16:41VUE

实现基础分页功能

在Vue中实现简单分页需要几个核心步骤:数据分片、页码计算、翻页逻辑。以下是一个基于Vue 3的示例实现方案:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [], // 原始数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.data.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  },
  async created() {
    // 模拟API获取数据
    this.data = Array.from({ length: 100 }, (_, i) => ({
      id: i + 1,
      name: `项目 ${i + 1}`
    }))
  }
}
</script>

添加页码选择器

扩展基础分页功能,添加页码跳转选择:

<template>
  <div>
    <!-- 数据列表同上 -->
    <div class="pagination">
      <button @click="goToPage(1)" :disabled="currentPage === 1">首页</button>
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>

      <span v-for="page in displayedPages" :key="page">
        <button 
          @click="goToPage(page)" 
          :class="{ active: currentPage === page }"
        >
          {{ page }}
        </button>
      </span>

      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
      <button @click="goToPage(totalPages)" :disabled="currentPage === totalPages">末页</button>
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    displayedPages() {
      const range = 2 // 显示当前页前后的页数
      let start = Math.max(1, this.currentPage - range)
      let end = Math.min(this.totalPages, this.currentPage + range)

      if (this.currentPage - start < range) {
        end = Math.min(this.totalPages, end + (range - (this.currentPage - start)))
      }
      if (end - this.currentPage < range) {
        start = Math.max(1, start - (range - (end - this.currentPage)))
      }

      return Array.from({ length: end - start + 1 }, (_, i) => start + i)
    }
  },
  methods: {
    goToPage(page) {
      this.currentPage = page
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用分页组件库

对于生产环境,推荐使用成熟的分页组件库:

vue实现简单分页

  1. 安装Element Plus分页组件:

    npm install element-plus
  2. 使用示例:

    vue实现简单分页

    
    <template>
    <el-pagination
     v-model:currentPage="currentPage"
     :page-size="itemsPerPage"
     :total="data.length"
     layout="prev, pager, next, jumper"
     @current-change="handleCurrentChange"
    />
    </template>
import { ElPagination } from 'element-plus'

export default { components: { ElPagination }, methods: { handleCurrentChange(val) { this.currentPage = val } } }

```

分页与API结合

实际项目中通常需要与后端API配合实现分页:

methods: {
  async fetchData(page = 1) {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page,
          per_page: this.itemsPerPage
        }
      })
      this.data = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  }
},
created() {
  this.fetchData()
}

性能优化建议

对于大数据量分页,考虑以下优化方案:

  • 使用虚拟滚动代替传统分页
  • 实现前端缓存已加载的页码数据
  • 添加加载状态指示器
  • 实现防抖处理快速翻页操作
data() {
  return {
    isLoading: false,
    cachedPages: {}
  }
},
methods: {
  async loadPage(page) {
    if (this.cachedPages[page]) {
      this.paginatedData = this.cachedPages[page]
      return
    }

    this.isLoading = true
    try {
      const data = await fetchPageData(page)
      this.cachedPages[page] = data
      this.paginatedData = data
    } finally {
      this.isLoading = false
    }
  }
}

标签: 分页简单
分享给朋友:

相关文章

vue 简单实现

vue 简单实现

Vue 简单实现 Vue 是一个渐进式 JavaScript 框架,适合快速构建用户界面。以下是实现一个简单 Vue 应用的步骤: 安装 Vue 通过 CDN 引入 Vue 是最简单的方式: &…

vue简单实现

vue简单实现

Vue 简单实现步骤 安装 Vue.js 最新版本: npm install vue@latest 创建 Vue 实例: import { createApp } from 'vue' cons…

php实现分页功能

php实现分页功能

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

vue实现简单树

vue实现简单树

使用 Vue 实现简单树形结构 递归组件实现 递归组件是 Vue 实现树形结构的常见方式。通过组件调用自身实现无限层级嵌套。 <template> <ul> &l…

vue page 实现分页

vue page 实现分页

实现 Vue 分页功能的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用 el-pagination 组件实现分页功能。 <template>…

vue 实现分页效果

vue 实现分页效果

使用 Element UI 实现分页 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI: npm install…