当前位置:首页 > 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;
}

异步数据加载

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

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()
  }
}

注意事项

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

原生vue实现表格分页

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

相关文章

vue实现分页功能

vue实现分页功能

Vue 分页功能实现 在 Vue 中实现分页功能通常需要结合后端 API 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑。…

vue 表格实现

vue 表格实现

Vue 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDa…

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalL…

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm inst…

vue组件传值实现分页

vue组件传值实现分页

Vue组件传值实现分页的方法 在Vue中实现分页功能,通常需要父子组件之间的数据传递。以下是几种常见的传值方式: 使用props传递数据 父组件通过props向子组件传递当前页码和总页数等分页信息…

vue 实现分页

vue 实现分页

分页实现方法 在Vue中实现分页功能,可以通过计算属性和方法结合来实现。以下是一个典型的分页实现方式: 模板部分 <template> <div> <…