当前位置:首页 > VUE

vue 分页如何实现

2026-02-18 17:37:41VUE

Vue 分页实现方法

基础分页实现

使用 v-for 渲染分页按钮,结合计算属性动态显示数据。假设 currentPage 是当前页码,totalPages 是总页数:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <div class="pagination">
      <button 
        v-for="page in totalPages" 
        :key="page"
        @click="currentPage = page"
        :class="{ active: currentPage === page }"
      >
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [], // 原始数据
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    }
  }
};
</script>

使用第三方库

对于复杂分页需求,可以使用 vue-paginateelement-ui 的分页组件。以 element-ui 为例:

<template>
  <div>
    <el-table :data="paginatedData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="itemsPerPage"
      :total="totalItems"
      layout="prev, pager, next"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      currentPage: 1,
      itemsPerPage: 10
    };
  },
  computed: {
    totalItems() {
      return this.data.length;
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }
};
</script>

后端分页处理

当数据量较大时,建议通过 API 实现后端分页。通过传递 pagepageSize 参数获取分页数据:

methods: {
  fetchData() {
    axios.get('/api/items', {
      params: {
        page: this.currentPage,
        pageSize: this.itemsPerPage
      }
    }).then(response => {
      this.data = response.data.items;
      this.totalItems = response.data.total;
    });
  }
}

分页样式优化

为分页按钮添加基础样式,提升用户体验:

vue 分页如何实现

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}

分享给朋友:

相关文章

vue如何实现默认页面

vue如何实现默认页面

Vue 实现默认页面的方法 在 Vue 项目中,通常需要设置一个默认页面(如首页或登录页)作为用户访问时的初始页面。可以通过路由配置实现。 配置路由默认跳转 在 Vue Router 中,使用 re…

vue如何实现登录

vue如何实现登录

Vue 实现登录功能的方法 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,通常命名为 Login.vue。表单包含用户名和密码输入框,以及提交按钮。 <template>…

java如何实现多态

java如何实现多态

多态的概念 多态是面向对象编程的三大特性之一(封装、继承、多态),指同一操作作用于不同对象时,可以产生不同的行为。Java中主要通过方法重写(Override)和接口/抽象类实现多态。 实现多态的三…

java如何实现多线程

java如何实现多线程

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

vue如何实现递归

vue如何实现递归

递归组件的实现方法 在Vue中实现递归组件通常用于渲染树形结构或嵌套数据。核心思路是组件在其模板中调用自身,但需注意终止条件以避免无限循环。 定义递归组件 组件需设置name选项,才能在模板中调用自…

vue如何实现404

vue如何实现404

实现 Vue 404 页面的方法 在 Vue 中实现 404 页面通常需要结合路由配置和动态路由匹配。以下是几种常见方法: 使用通配符路由匹配 在 Vue Router 配置中,可以通过 * 通配符…