当前位置:首页 > 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实现表格上传头像

实现表格上传头像功能 在Vue中实现表格上传头像功能,可以通过以下步骤完成。这里假设使用Element UI作为UI框架,但方法同样适用于其他UI库或原生实现。 安装必要依赖 确保项目中已安装Ele…

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

vue elementui实现分页

vue elementui实现分页

Vue + ElementUI 分页实现方法 安装与引入 确保已安装 ElementUI,可通过 npm 或 yarn 安装: npm install element-ui 在 main.js 中全…

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,包括边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…

vue分页实现原理

vue分页实现原理

Vue 分页实现原理 Vue 分页的核心原理是通过计算当前页的数据范围,动态渲染数据列表并控制分页组件的交互。以下是关键实现步骤: 数据分片计算 分页需要根据当前页码和每页条数截取数据。假设原始数据…