当前位置:首页 > VUE

vue实现table表格分页

2026-01-21 01:39:29VUE

实现基础表格分页

在Vue中实现表格分页需要结合el-tableel-pagination组件(Element UI为例)。以下是一个基础实现方案:

<template>
  <div>
    <el-table :data="currentPageData" border>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>

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

<script>
export default {
  data() {
    return {
      tableData: [], // 全部数据
      currentPageData: [], // 当前页数据
      currentPage: 1,
      pageSize: 5,
      total: 0
    }
  },
  methods: {
    fetchData() {
      // 模拟API请求
      this.tableData = Array.from({length: 23}, (_, i) => ({
        name: `用户${i+1}`,
        age: Math.floor(Math.random() * 30) + 20
      }))
      this.total = this.tableData.length
      this.updatePageData()
    },
    updatePageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.currentPageData = this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.updatePageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.updatePageData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

服务器端分页实现

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

vue实现table表格分页

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

自定义分页样式

可以通过CSS自定义分页组件样式:

vue实现table表格分页

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #409EFF;
  color: #fff;
}

分页与表格联动优化

添加加载状态提升用户体验:

<el-table v-loading="loading" :data="currentPageData">
  <!-- 表格列定义 -->
</el-table>

<script>
export default {
  data() {
    return {
      loading: false
    }
  },
  methods: {
    async fetchData() {
      this.loading = true
      try {
        // 数据获取逻辑
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

分页缓存策略

对于频繁访问的页面,可以添加缓存机制:

const pageCache = {}
methods: {
  async fetchData() {
    const cacheKey = `${this.currentPage}-${this.pageSize}`
    if (pageCache[cacheKey]) {
      this.currentPageData = pageCache[cacheKey]
      return
    }

    const res = await axios.get('/api/data', {
      params: { page: this.currentPage, size: this.pageSize }
    })

    pageCache[cacheKey] = res.data.list
    this.currentPageData = res.data.list
    this.total = res.data.total
  }
}

以上实现方案可以根据具体项目需求进行调整,如添加排序功能、筛选条件等与分页的联动处理。

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

相关文章

vue前端分页怎么实现

vue前端分页怎么实现

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

vue组件传值实现分页

vue组件传值实现分页

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

h5实现表格样式

h5实现表格样式

实现H5表格样式的方法 使用HTML和CSS创建基础表格 在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th>…

vue router实现分页

vue router实现分页

使用 Vue Router 实现分页 在 Vue.js 项目中,可以通过 Vue Router 结合分页组件实现分页功能。以下是实现分页的几种方法: 使用路由参数传递页码 在路由配置中定义一个动态参…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…

react如何实现分页

react如何实现分页

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