当前位置:首页 > VUE

vue实现表格分页组件

2026-01-22 16:17:31VUE

Vue实现表格分页组件的方法

安装依赖(如需要) 若项目未安装Element UI或类似UI库,可通过以下命令安装:

npm install element-ui

基础分页组件结构 使用Element UI的el-pagination组件:

vue实现表格分页组件

<template>
  <div>
    <el-table :data="currentPageData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

数据逻辑处理

<script>
export default {
  data() {
    return {
      tableData: [], // 全部数据
      currentPageData: [], // 当前页数据
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.updateCurrentPageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.updateCurrentPageData()
    },
    updateCurrentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.currentPageData = this.tableData.slice(start, end)
    },
    fetchData() {
      // 模拟API请求
      this.tableData = Array.from({length: 100}, (_,i) => ({id: i+1}))
      this.total = this.tableData.length
      this.updateCurrentPageData()
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

自定义样式(可选)

vue实现表格分页组件

<style scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>

纯Vue实现方案(不依赖UI库)

<template>
  <div>
    <table>
      <tr v-for="item in currentPageData" :key="item.id">
        <!-- 表格内容 -->
      </tr>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第{{ currentPage }}页</span>
      <button @click="nextPage" :disabled="currentPage >= totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    totalPages() {
      return Math.ceil(this.tableData.length / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

关键点说明

  • 分页核心逻辑是通过数组的slice方法截取当前页数据
  • currentPagepageSize变化时需重新计算当前页数据
  • 分页器事件需绑定size-changecurrent-change
  • 总条目数total需与后端数据同步(若为异步请求)

标签: 分页组件
分享给朋友:

相关文章

elementui组件

elementui组件

ElementUI 组件概述 ElementUI 是一套基于 Vue.js 的桌面端组件库,提供丰富的 UI 组件,适用于快速开发中后台系统。以下是其核心组件分类及典型用法。 基础组件 Button…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

vue 分页功能实现

vue 分页功能实现

分页功能实现方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。 安装 Element UI: npm in…

vue加载实现分页

vue加载实现分页

Vue 实现分页加载的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用其分页组件。在模板中添加分页组件,并绑定相关事件和数据。 <template&g…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> <d…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-aliv…