当前位置:首页 > VUE

原生vue实现表格分页

2026-02-24 07:35:55VUE

原生 Vue 实现表格分页的方法

数据准备与分页逻辑

在 Vue 的 data 中定义表格数据和分页相关变量:

data() {
  return {
    tableData: [],       // 全部数据
    currentPage: 1,      // 当前页码
    pageSize: 10,        // 每页显示条数
    total: 0             // 总数据量
  }
}

计算当前页数据

使用计算属性筛选当前页显示的数据:

computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.pageSize
    const end = start + this.pageSize
    return this.tableData.slice(start, end)
  }
}

分页控件实现

在模板中添加分页按钮和表格:

<template>
  <div>
    <table>
      <tr v-for="(item, index) in paginatedData" :key="index">
        <!-- 表格内容 -->
      </tr>
    </table>

    <div class="pagination">
      <button 
        @click="currentPage--" 
        :disabled="currentPage === 1">
        上一页
      </button>
      <span>第 {{ currentPage }} 页</span>
      <button 
        @click="currentPage++" 
        :disabled="currentPage >= Math.ceil(total / pageSize)">
        下一页
      </button>
    </div>
  </div>
</template>

样式优化(可选)

添加基础样式改善视觉效果:

.pagination {
  margin-top: 20px;
  display: flex;
  gap: 10px;
  align-items: center;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

异步数据加载

如果数据需要从接口获取:

原生vue实现表格分页

methods: {
  async fetchData() {
    const res = await api.getData()
    this.tableData = res.data
    this.total = res.total
  }
},
mounted() {
  this.fetchData()
}

完整组件示例

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.tableData.slice(start, end)
    }
  },
  methods: {
    async fetchData() {
      const res = await api.getData()
      this.tableData = res.data
      this.total = res.total
    }
  },
  mounted() {
    this.fetchData()
  }
}

注意事项

  • 分页按钮的禁用状态需要根据当前页码动态计算
  • 对于大数据量建议使用服务端分页
  • 可以添加页码跳转功能增强用户体验

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

相关文章

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

vue  实现分页

vue 实现分页

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

vue实现表格追加

vue实现表格追加

实现表格数据追加的方法 在Vue中实现表格数据追加可以通过多种方式完成,主要涉及数据绑定和数组操作。以下是几种常见的方法: 数据绑定与v-for指令 通过v-for指令渲染表格数据,利用数组的co…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…

vue实现表格输入

vue实现表格输入

Vue实现表格输入的方法 基础表格实现 使用v-for指令动态渲染表格行和列,结合v-model实现双向数据绑定。 <template> <table> <…