当前位置:首页 > 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显示当前页数据:

用vue实现前端分页

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

动态调整页码

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

用vue实现前端分页

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>

完整组件示例

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

<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 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keywor…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…