当前位置:首页 > VUE

vue 实现页面 表格 分页

2026-01-21 14:26:08VUE

Vue 实现表格分页的方法

在 Vue 中实现表格分页通常需要结合后端分页数据或前端分页逻辑。以下是两种常见的实现方式:

前端分页实现

前端分页适用于数据量较小的情况,数据一次性加载到前端后通过计算实现分页。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th v-for="col in columns" :key="col">{{ col }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in paginatedData" :key="item.id">
          <td v-for="col in columns" :key="col">{{ item[col] }}</td>
        </tr>
      </tbody>
    </table>

    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: [], // 全部数据
      columns: ['id', 'name', 'age'], // 表格列
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.allData.length / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.pageCount) this.currentPage++
    }
  },
  async created() {
    // 获取数据
    this.allData = await fetchData()
  }
}
</script>

后端分页实现

后端分页适用于数据量大的情况,每次只请求当前页的数据。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th v-for="col in columns" :key="col">{{ col }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in tableData" :key="item.id">
          <td v-for="col in columns" :key="col">{{ item[col] }}</td>
        </tr>
      </tbody>
    </table>

    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页 (共 {{ totalPage }} 页)</span>
      <button @click="nextPage" :disabled="currentPage === totalPage">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [], // 当前页数据
      columns: ['id', 'name', 'age'],
      currentPage: 1,
      pageSize: 10,
      totalPage: 1
    }
  },
  methods: {
    async fetchPageData() {
      const res = await fetch(`/api/data?page=${this.currentPage}&size=${this.pageSize}`)
      const data = await res.json()
      this.tableData = data.items
      this.totalPage = data.totalPage
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.fetchPageData()
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPage) {
        this.currentPage++
        this.fetchPageData()
      }
    }
  },
  created() {
    this.fetchPageData()
  }
}
</script>

使用 Element UI 的分页组件

如果使用 Element UI,可以更简单地实现分页功能:

vue 实现页面 表格 分页

<template>
  <div>
    <el-table :data="tableData">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>

    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    async fetchData() {
      const res = await fetch(`/api/data?page=${this.currentPage}&size=${this.pageSize}`)
      const data = await res.json()
      this.tableData = data.items
      this.total = data.total
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

以上方法可以根据实际需求选择合适的方式实现 Vue 中的表格分页功能。前端分页适合数据量小的情况,后端分页适合大数据量场景,而使用 UI 组件库可以快速实现美观的分页效果。

标签: 分页表格
分享给朋友:

相关文章

vue实现动态用户表格

vue实现动态用户表格

Vue 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

vue实现上传表格

vue实现上传表格

Vue实现上传表格的方法 使用Element UI上传组件 Element UI提供了el-upload组件,可以方便地实现文件上传功能。需要安装Element UI并引入相关组件。 安装Eleme…

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…

vue实现表格输出

vue实现表格输出

Vue 实现表格输出的方法 使用原生表格标签 在 Vue 模板中可以直接使用 HTML 原生表格标签(<table>、<tr>、<td>等)渲染数据。通过 v-fo…

vue 实现页面 表格

vue 实现页面 表格

Vue 实现页面表格的方法 Vue 提供了多种方式实现表格功能,可以根据需求选择不同的方案。以下是常见的实现方法: 使用原生 HTML 表格 通过 Vue 的模板语法直接渲染表格数据: <t…