当前位置:首页 > VUE

vue翻页实现

2026-01-07 21:23:39VUE

Vue翻页实现方法

基础分页组件实现

使用v-for和计算属性实现基础分页逻辑:

<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 }} 页</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </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() {
      this.currentPage++
    },
    prevPage() {
      this.currentPage--
    }
  }
}
</script>

使用第三方库

对于复杂分页需求,可以使用专门的分页库:

npm install vuejs-paginate

实现示例:

<template>
  <div>
    <paginate
      :page-count="totalPages"
      :click-handler="changePage"
      :prev-text="'<'"
      :next-text="'>'"
      :container-class="'pagination'"
    >
    </paginate>
  </div>
</template>

<script>
import Paginate from 'vuejs-paginate'
export default {
  components: { Paginate },
  methods: {
    changePage(pageNum) {
      this.currentPage = pageNum
    }
  }
}
</script>

服务器端分页

当数据量较大时,建议采用API分页:

methods: {
  async fetchData(page = 1) {
    const res = await axios.get(`/api/items?page=${page}&limit=10`)
    this.items = res.data.items
    this.totalPages = res.data.totalPages
  }
}

分页样式优化

添加CSS美化分页控件:

.pagination {
  display: flex;
  list-style: none;
  padding: 0;
}
.pagination li {
  margin: 0 5px;
  cursor: pointer;
}
.pagination li.active {
  font-weight: bold;
}

动态分页大小

允许用户选择每页显示数量:

vue翻页实现

<select v-model="itemsPerPage" @change="resetPagination">
  <option value="5">5条/页</option>
  <option value="10">10条/页</option>
  <option value="20">20条/页</option>
</select>

methods: {
  resetPagination() {
    this.currentPage = 1
  }
}

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

相关文章

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现换页

vue实现换页

Vue实现页面切换的方法 在Vue中实现页面切换通常可以通过Vue Router来完成。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是几种常见的实现方式:…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…