当前位置:首页 > VUE

vue实现表格分页组件

2026-01-22 16:17:31VUE

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

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

npm install element-ui

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

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

自定义样式(可选)

<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需与后端数据同步(若为异步请求)

vue实现表格分页组件

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

相关文章

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分…

vue实现折叠组件

vue实现折叠组件

Vue 折叠组件实现方法 基于 v-show 和事件触发 通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。 <template> <div> &…

vue 实现组件刷新

vue 实现组件刷新

实现组件刷新的方法 在Vue中,组件刷新通常指重新渲染组件或重置组件状态。以下是几种常见的实现方式: 使用v-if控制渲染 通过v-if指令可以销毁并重新创建组件: <template&…

vue前端分页怎么实现

vue前端分页怎么实现

使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 <el-pagination>,可以快速实现分页功能。需要安装 Element UI 并注册组件。 &…

elementui组件

elementui组件

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

vue  实现分页

vue 实现分页

实现分页功能 在Vue中实现分页功能通常需要结合后端API或前端数据处理。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,所有数据一次性加载到前端后进行分页处理: <t…