当前位置:首页 > VUE

vue 实现页面 表格 分页

2026-02-22 05:48:25VUE

Vue 实现表格分页的方法

基础实现

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

npm install element-ui  # 以Element UI为例

在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:

<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实现后台分页

vue实现后台分页

实现后台分页的方法 在Vue中实现后台分页通常需要结合前端和后端逻辑,后端负责处理数据分页,前端负责展示分页数据和交互。以下是具体实现方法: 后端API设计 后端需要提供支持分页的API接口,通常包…

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

vue实现表格多行修改

vue实现表格多行修改

实现表格多行修改的方法 数据绑定与表格渲染 使用v-model绑定表格数据到Vue实例的data属性。通过v-for循环渲染表格行,并为每行数据添加编辑状态标识。 <template>…

vue分页实现

vue分页实现

Vue 分页实现方法 基础分页组件实现 创建一个简单的分页组件,包含上一页、下一页和页码按钮。组件接收当前页码、总页数等参数,通过事件通知父组件页码变化。 <template> &l…

css 制作表格

css 制作表格

CSS 制作表格的方法 使用 CSS 制作表格可以通过多种方式实现,包括原生 HTML 表格样式、Flexbox 布局或 Grid 布局。以下是几种常见的方法: 原生 HTML 表格样式 HTML…

vue elementui实现分页

vue elementui实现分页

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