当前位置:首页 > VUE

vue普通表格实现分页

2026-02-23 23:24:05VUE

Vue 表格分页实现方法

在 Vue 中实现表格分页可以通过多种方式完成,以下是几种常见的方法:

使用 Element UI 分页组件

安装 Element UI 后可以直接使用其分页组件:

<template>
  <div>
    <el-table :data="currentTableData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [], // 全部数据
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    currentTableData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    }
  },
  created() {
    // 获取数据
    fetchData().then(data => {
      this.tableData = data
      this.total = data.length
    })
  }
}
</script>

手动实现分页功能

如果不使用 UI 库,可以手动实现分页:

<template>
  <div>
    <table>
      <!-- 表格内容 -->
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</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 {
      allData: [],
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.allData.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.allData.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用 Vue 插件

可以使用专门的分页插件如 vuejs-paginate

// 安装插件
npm install vuejs-paginate

// 使用示例
<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <paginate
      v-model="currentPage"
      :page-count="totalPages"
      :click-handler="changePage"
      :prev-text="'Prev'"
      :next-text="'Next'"
      :container-class="'pagination'">
    </paginate>
  </div>
</template>

<script>
import Paginate from 'vuejs-paginate'
export default {
  components: {
    Paginate
  },
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      allItems: []
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allItems.length / this.itemsPerPage)
    }
  },
  methods: {
    changePage(pageNum) {
      this.currentPage = pageNum
    }
  }
}
</script>

后端分页处理

对于大数据量,建议使用后端分页:

vue普通表格实现分页

<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <div class="pagination">
      <!-- 分页控件 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    fetchData() {
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      axios.get('/api/data', { params })
        .then(response => {
          this.tableData = response.data.items
          this.total = response.data.total
        })
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

选择哪种方法取决于项目需求和技术栈。UI 库提供现成解决方案,手动实现更灵活,后端分页适合大数据量场景。

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

相关文章

vue实现下拉分页思想

vue实现下拉分页思想

实现下拉分页的基本思想 下拉分页(Infinite Scroll)是一种常见的分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 中可以通过监听滚动事件或使用第三方库实现。 监听滚动事件…

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

css制作表格

css制作表格

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

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子来…

用vue实现表格

用vue实现表格

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

vue加载实现分页

vue加载实现分页

Vue 实现分页加载的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用其分页组件。在模板中添加分页组件,并绑定相关事件和数据。 <template&g…