当前位置:首页 > VUE

vue实现分页表

2026-02-18 05:15:38VUE

Vue实现分页表格的方法

使用Element UI的分页组件

安装Element UI库后,可以通过el-tableel-pagination组件快速实现分页表格功能。

<template>
  <div>
    <el-table :data="tableData" border>
      <el-table-column prop="date" label="日期"></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, 30]"
      :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
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      // 模拟API请求
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.tableData = mockData.slice(start, end)
      this.total = mockData.length
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

自定义分页实现

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

<template>
  <div>
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>

    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第{{ currentPage }}页</span>
    <button @click="nextPage" :disabled="currentPage >= totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: [],
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      this.currentPage--
    },
    nextPage() {
      this.currentPage++
    }
  },
  created() {
    // 获取数据
    this.allData = [...]
  }
}
</script>

结合后端API的分页实现

实际项目中通常需要从后端获取分页数据。

methods: {
  async fetchData() {
    try {
      const res = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.tableData = res.data.items
      this.total = res.data.total
    } catch (error) {
      console.error(error)
    }
  }
}

分页器样式自定义

可以通过CSS对分页组件进行样式调整。

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

这些方法提供了从简单到复杂的Vue分页表格实现方案,可根据项目需求选择合适的实现方式。

vue实现分页表

标签: 分页vue
分享给朋友:

相关文章

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sel…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.definePropert…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…