当前位置:首页 > VUE

vue如何实现列表分页

2026-01-21 04:20:03VUE

Vue实现列表分页的方法

使用计算属性分页

通过计算属性对数据进行切片处理,结合页码和每页数量实现分页。

data() {
  return {
    items: [], // 原始数据
    currentPage: 1,
    perPage: 10
  }
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.perPage
    const end = start + this.perPage
    return this.items.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.items.length / this.perPage)
  }
}

使用第三方库

可以集成专门的分页组件库如vue-paginateelement-ui的分页组件。

安装vue-paginate

npm install vue-paginate

基本用法:

import VuePaginate from 'vue-paginate'
Vue.use(VuePaginate)

// 模板中使用
<template>
  <paginate
    :page-count="totalPages"
    :click-handler="changePage"
    :container-class="'pagination'"
  />
</template>

服务器端分页

当数据量很大时,建议使用服务器端分页,通过API传递页码和每页数量参数。

methods: {
  fetchData(page = 1) {
    axios.get(`/api/items?page=${page}&per_page=${this.perPage}`)
      .then(response => {
        this.items = response.data.items
        this.totalPages = response.data.total_pages
      })
  },
  changePage(pageNum) {
    this.currentPage = pageNum
    this.fetchData(pageNum)
  }
}

完整组件示例

<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>

<script>
export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      perPage: 10
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.perPage
      const end = start + this.perPage
      return this.items.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.items.length / this.perPage)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    }
  },
  created() {
    // 获取初始数据
    this.items = /* 获取数据逻辑 */;
  }
}
</script>

样式处理

可以为分页组件添加CSS样式增强用户体验:

vue如何实现列表分页

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

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

分享给朋友:

相关文章

vue如何实现id

vue如何实现id

在 Vue 中实现 ID 绑定 Vue 提供了多种方式为元素或组件绑定唯一的 ID,可以根据具体需求选择合适的方法。 方法一:使用 v-bind 绑定静态或动态 ID <template&g…

vue实现分页

vue实现分页

Vue 实现分页的方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI:…

js如何实现继承

js如何实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例能够访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

vue如何实现滤镜

vue如何实现滤镜

Vue 实现滤镜的方法 在 Vue 中实现滤镜效果可以通过多种方式,以下是常见的几种方法: 使用 CSS filter 属性 通过 CSS 的 filter 属性可以直接为元素添加滤镜效果。在 Vu…

vue如何实现原理

vue如何实现原理

Vue 实现原理的核心机制 Vue.js 的核心实现原理基于响应式系统、虚拟 DOM 和组件化设计。以下是关键机制的详细解析: 响应式系统 Vue 通过 Object.defineProperty(…

java如何实现多线程

java如何实现多线程

实现多线程的方法 在Java中,实现多线程主要有两种方式:继承Thread类和实现Runnable接口。此外,还可以使用ExecutorService等高级工具类来管理线程。 继承Thread类 通…