当前位置:首页 > VUE

vue实现前端分页

2026-03-29 13:30:17VUE

Vue 前端分页实现方法

基础数据分页

  1. 准备分页数据

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

    vue实现前端分页

    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. 分页控件模板

    vue实现前端分页

    <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. 使用分页组件

    <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 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现公式

vue实现公式

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

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 get…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…