当前位置:首页 > 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需与后端数据同步(若为异步请求)

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

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

vue抽屉组件实现

vue抽屉组件实现

Vue 抽屉组件实现 使用 Element UI 实现 Element UI 提供了现成的抽屉组件 el-drawer,可以快速实现抽屉效果。 安装 Element UI: npm install…

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现组件刷新

vue实现组件刷新

强制重新渲染组件 使用 this.$forceUpdate() 方法可以强制 Vue 实例重新渲染,但不会更新子组件。适用于依赖非响应式数据的情况。 methods: { refreshComp…