当前位置:首页 > VUE

用vue实现前端分页

2026-02-24 18:42:38VUE

实现前端分页的基本思路

在Vue中实现前端分页通常涉及以下核心逻辑:将总数据分割为多个页面,根据当前页码动态显示对应数据,并提供分页控件供用户切换页面。以下是一个清晰的实现方案。

准备分页数据

假设已有从API或本地获取的完整数据列表totalData,需定义分页相关变量:

data() {
  return {
    totalData: [],      // 所有数据
    currentPage: 1,     // 当前页码
    pageSize: 10,       // 每页显示条数
    paginatedData: []   // 当前页数据
  }
}

计算分页数据

使用计算属性动态筛选当前页数据,避免直接操作原始数据:

computed: {
  totalPages() {
    return Math.ceil(this.totalData.length / this.pageSize);
  },
  displayedData() {
    const start = (this.currentPage - 1) * this.pageSize;
    const end = start + this.pageSize;
    return this.totalData.slice(start, end);
  }
}

渲染分页内容

在模板中遍历displayedData显示当前页数据:

<template>
  <ul>
    <li v-for="item in displayedData" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

添加分页控件

创建分页按钮组,允许用户切换页面:

<div class="pagination">
  <button 
    @click="currentPage = 1" 
    :disabled="currentPage === 1">
    首页
  </button>
  <button 
    @click="currentPage--" 
    :disabled="currentPage === 1">
    上一页
  </button>

  <span v-for="page in totalPages" :key="page">
    <button 
      @click="currentPage = page"
      :class="{ active: currentPage === page }">
      {{ page }}
    </button>
  </span>

  <button 
    @click="currentPage++" 
    :disabled="currentPage === totalPages">
    下一页
  </button>
  <button 
    @click="currentPage = totalPages" 
    :disabled="currentPage === totalPages">
    末页
  </button>
</div>

样式优化

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

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

动态调整页码

监听页码变化,确保其在有效范围内:

watch: {
  currentPage(newVal) {
    if (newVal < 1) this.currentPage = 1;
    if (newVal > this.totalPages) this.currentPage = this.totalPages;
  }
}

使用第三方库(可选)

如需快速实现复杂分页,可考虑使用现成组件库:

npm install element-plus

示例代码:

<template>
  <el-pagination
    v-model:current-page="currentPage"
    :page-size="pageSize"
    :total="totalData.length"
    layout="prev, pager, next"
  />
</template>

完整组件示例

整合上述代码的完整单文件组件:

用vue实现前端分页

<template>
  <div>
    <ul>
      <li v-for="item in displayedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <div class="pagination">
      <button @click="currentPage = 1" :disabled="currentPage === 1">首页</button>
      <button @click="currentPage--" :disabled="currentPage === 1">上一页</button>

      <span v-for="page in totalPages" :key="page">
        <button 
          @click="currentPage = page"
          :class="{ active: currentPage === page }">
          {{ page }}
        </button>
      </span>

      <button @click="currentPage++" :disabled="currentPage === totalPages">下一页</button>
      <button @click="currentPage = totalPages" :disabled="currentPage === totalPages">末页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      totalData: Array.from({ length: 50 }, (_, i) => ({ id: i, name: `项目 ${i}` })),
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalData.length / this.pageSize);
    },
    displayedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.totalData.slice(start, end);
    }
  },
  watch: {
    currentPage(newVal) {
      if (newVal < 1) this.currentPage = 1;
      if (newVal > this.totalPages) this.currentPage = this.totalPages;
    }
  }
}
</script>

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

相关文章

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue弹幕实现

vue弹幕实现

Vue弹幕实现方法 弹幕功能常见于视频直播或评论区,以下是基于Vue的实现方案,涵盖基础弹幕、动画控制及性能优化。 基础弹幕渲染 通过v-for动态渲染弹幕列表,结合CSS实现横向移动效果。弹幕数据…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…

前端vue实现概率

前端vue实现概率

Vue实现概率功能的方法 在Vue中实现概率功能通常涉及随机数生成和概率计算。以下是几种常见实现方式: 基础概率实现 使用Math.random()生成0到1之间的随机数,与设定概率比较:…