当前位置:首页 > 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中使用setInterval和clearInterval实现定时器功能。在组件的data中定义计时器变量,mounted钩子中启动定时器,beforeDestroy钩子中清除定…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <tem…

vue elementui实现分页

vue elementui实现分页

使用 Element UI 实现 Vue 分页功能 Element UI 提供了 el-pagination 组件,可以方便地实现分页功能。以下是具体实现步骤: 安装 Element UI 确保项…

vue实现后台分页

vue实现后台分页

实现后台分页的方法 在Vue中实现后台分页通常需要结合前端和后端逻辑,后端负责处理数据分页,前端负责展示分页数据和交互。以下是具体实现方法: 后端API设计 后端需要提供支持分页的API接口,通常包…

vue实现简单的弹窗

vue实现简单的弹窗

Vue实现简单弹窗的方法 使用v-if和v-show控制显示 通过Vue的指令v-if或v-show可以轻松控制弹窗的显示与隐藏。v-if会完全销毁和重建DOM元素,而v-show仅通过CSS的d…

vue 实现简单登陆

vue 实现简单登陆

Vue 实现简单登录功能 创建 Vue 项目 使用 Vue CLI 创建一个新项目: vue create login-demo 选择默认配置或手动配置(推荐包含 Vue Router 和 Vuex…