当前位置:首页 > VUE

vue普通表格实现分页

2026-01-23 08:46:14VUE

实现Vue普通表格分页的方法

在Vue中实现表格分页功能,可以通过以下步骤完成:

安装依赖(可选) 如果项目需要第三方分页组件,可以安装如element-uivxe-table等UI库。以element-ui为例:

npm install element-ui

基础分页实现 在Vue组件中,需要定义数据、分页参数和计算方法:

<template>
  <div>
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </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 {
      tableData: [], // 原始数据
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.tableData.length / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.tableData.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用Element UI分页组件 如果使用Element UI,可以简化分页实现:

<template>
  <div>
    <el-table :data="paginatedData">
      <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="tableData.length">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.tableData.slice(start, end)
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

服务器端分页 对于大数据量,建议使用服务器端分页:

methods: {
  fetchData(page = 1) {
    axios.get(`/api/data?page=${page}&size=${this.pageSize}`)
      .then(response => {
        this.tableData = response.data.items
        this.total = response.data.total
      })
  },
  handleCurrentChange(val) {
    this.fetchData(val)
  }
}

样式优化 可以添加CSS美化分页控件:

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

以上方法可以根据项目需求选择适合的实现方式,基础分页适合简单场景,UI库组件提供更丰富的功能,服务器端分页适合大数据量情况。

vue普通表格实现分页

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

相关文章

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Contex…

css制作表格

css制作表格

CSS 制作表格的方法 使用 CSS 可以灵活地创建和样式化表格,以下是几种常见的方法: 使用 HTML 表格标签结合 CSS 样式 通过 HTML 的 <table>、<tr&…

用vue实现表格

用vue实现表格

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以轻松渲染表格数据。准备一个数据数组,在模板中用 v-for 循环渲染。 <template> <tab…

vue实现表格内容

vue实现表格内容

Vue实现表格内容的方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签构建表格结构,通过v-for指令动态渲染数据。 <template> &…