当前位置:首页 > VUE

vue 实现分页

2026-01-12 21:45:51VUE

分页实现方法

在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">Previous</button>
      <span>Page {{ currentPage }} of {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
    </div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      items: [], // 你的数据数组
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用第三方库

对于更复杂的分页需求,可以考虑使用专门的分页组件库:

  1. vue-paginate:轻量级分页组件

    npm install vue-paginate
  2. Element UI Pagination:如果你使用Element UI

    <el-pagination
    layout="prev, pager, next"
    :total="1000"
    :page-size="10"
    @current-change="handleCurrentChange">
    </el-pagination>
  3. Vuetify Pagination:Material Design风格的分页

    <v-pagination
    v-model="page"
    :length="15"
    :total-visible="7"
    ></v-pagination>

服务器端分页

当数据量很大时,应该实现服务器端分页:

methods: {
  fetchData(page) {
    axios.get(`/api/items?page=${page}&limit=${this.itemsPerPage}`)
      .then(response => {
        this.items = response.data.items
        this.totalItems = response.data.total
      })
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData(page)
  }
}

样式优化

为分页组件添加基本样式:

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

.pagination button {
  margin: 0 10px;
  padding: 5px 10px;
  cursor: pointer;
}

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

高级功能实现

对于更高级的分页需求,可以添加以下功能:

  • 页码跳转输入框
  • 每页显示数量选择器
  • 总记录数显示
  • 首尾页快捷跳转按钮

这些功能可以通过扩展基本的分页组件来实现,根据具体需求调整计算属性和方法。

vue 实现分页

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

相关文章

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CS…