当前位置:首页 > VUE

vue 实现页面 表格 分页

2026-02-22 05:48:25VUE

Vue 实现表格分页的方法

基础实现

安装依赖(如使用第三方分页组件):

npm install element-ui  # 以Element UI为例

在Vue文件中引入表格和分页组件:

vue 实现页面 表格 分页

<template>
  <div>
    <el-table :data="currentTableData" border>
      <!-- 表格列定义 -->
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],     // 全部数据
      currentTableData: [], // 当前页数据
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    fetchData() {
      // 模拟API请求
      this.tableData = Array.from({length: 100}, (_,i) => ({id: i+1, name: `用户${i+1}`}))
      this.total = this.tableData.length
      this.updateCurrentData()
    },
    updateCurrentData() {
      const start = (this.currentPage - 1) * this.pageSize
      this.currentTableData = this.tableData.slice(start, start + this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.updateCurrentData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.updateCurrentData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

服务端分页实现

当数据量较大时,建议采用服务端分页:

methods: {
  async fetchData() {
    const res = await axios.get('/api/data', {
      params: {
        page: this.currentPage,
        size: this.pageSize
      }
    })
    this.currentTableData = res.data.list
    this.total = res.data.total
  },
  handleSizeChange(val) {
    this.pageSize = val
    this.fetchData()
  },
  handleCurrentChange(val) {
    this.currentPage = val
    this.fetchData()
  }
}

自定义分页组件

如需自定义分页UI:

vue 实现页面 表格 分页

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span v-for="page in visiblePages" :key="page">
      <button @click="goToPage(page)" :class="{active: currentPage === page}">
        {{ page }}
      </button>
    </span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  props: ['currentPage', 'total', 'pageSize'],
  computed: {
    totalPages() {
      return Math.ceil(this.total / this.pageSize)
    },
    visiblePages() {
      // 返回当前可见的页码数组
      const range = 2
      let start = Math.max(1, this.currentPage - range)
      let end = Math.min(this.totalPages, this.currentPage + range)
      return Array.from({length: end-start+1}, (_,i) => start+i)
    }
  },
  methods: {
    prevPage() {
      this.$emit('page-change', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-change', this.currentPage + 1)
    },
    goToPage(page) {
      this.$emit('page-change', page)
    }
  }
}
</script>

<style>
.pagination button.active {
  background-color: #409EFF;
  color: white;
}
</style>

性能优化技巧

对于大数据量的前端分页,可使用虚拟滚动技术:

// 使用vue-virtual-scroller插件
npm install vue-virtual-scroller

// 组件中使用
<template>
  <RecycleScroller
    class="scroller"
    :items="currentTableData"
    :item-size="50"
    key-field="id"
    v-slot="{ item }">
    <div class="item">
      {{ item.id }} - {{ item.name }}
    </div>
  </RecycleScroller>
</template>

注意事项

分页逻辑应与排序、筛选功能协同工作。在改变排序或筛选条件时,应重置当前页码为1。

对于移动端适配,可考虑使用无限滚动加载模式替代传统分页控件。

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

相关文章

vue前端分页怎么实现

vue前端分页怎么实现

使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 <el-pagination>,可以快速实现分页功能。需要安装 Element UI 并注册组件。…

vue实现下拉分页思想

vue实现下拉分页思想

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

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 Vue.js 提供了灵活的方式来实现表格功能,可以通过组合 v-for 指令和动态数据绑定快速构建表格。以下是几种常见的实现方法: 基础表格实现 通过 v-for 遍历数组…

vue实现表格追加

vue实现表格追加

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

vue实现列表分页

vue实现列表分页

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