当前位置:首页 > VUE

vue实现前端分页

2026-03-29 13:30:17VUE

Vue 前端分页实现方法

基础数据分页

  1. 准备分页数据

    data() {
    return {
     allItems: [], // 原始数据
     currentPage: 1, // 当前页码
     itemsPerPage: 10 // 每页条数
    }
    }
  2. 计算分页数据

    computed: {
    paginatedItems() {
     const start = (this.currentPage - 1) * this.itemsPerPage
     const end = start + this.itemsPerPage
     return this.allItems.slice(start, end)
    },
    totalPages() {
     return Math.ceil(this.allItems.length / this.itemsPerPage)
    }
    }
  3. 分页控件模板

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

高级分页组件

  1. 创建可复用分页组件

    // Pagination.vue
    props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
    },
    computed: {
    totalPages() {
     return Math.ceil(this.totalItems / this.itemsPerPage)
    }
    },
    methods: {
    changePage(page) {
     this.$emit('page-changed', page)
    }
    }
  2. 使用分页组件

    vue实现前端分页

    <pagination
    :total-items="allItems.length"
    :items-per-page="itemsPerPage"
    :current-page="currentPage"
    @page-changed="currentPage = $event"
    />

分页样式优化

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

分页逻辑优化

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

带页码显示的分页

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

与后端API结合

async fetchData() {
  const response = await axios.get('/api/items', {
    params: {
      page: this.currentPage,
      limit: this.itemsPerPage
    }
  })
  this.allItems = response.data.items
  this.totalItems = response.data.total
}

这些方法可以根据实际需求组合使用,实现从简单到复杂的前端分页功能。

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

相关文章

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template>…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…