当前位置:首页 > VUE

vue分页功能的实现

2026-02-21 22:22:13VUE

实现基础分页功能

使用v-pagination组件配合数据切片实现基础分页。需要计算总页数和当前页显示的数据范围。

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <v-pagination 
      v-model="currentPage"
      :length="totalPages"
    ></v-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: [], // 全部数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.allData.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.allData.slice(start, end)
    }
  }
}
</script>

结合API实现后端分页

当数据量较大时,建议采用后端分页。通过API传递页码和每页条数参数。

vue分页功能的实现

methods: {
  async fetchData() {
    const params = {
      page: this.currentPage,
      size: this.itemsPerPage
    }
    const res = await axios.get('/api/data', { params })
    this.paginatedData = res.data.items
    this.totalItems = res.data.total
  }
}

自定义分页样式

可以通过total-visible属性控制显示页码数量,使用prev-iconnext-icon自定义翻页按钮。

vue分页功能的实现

<v-pagination
  v-model="currentPage"
  :length="totalPages"
  :total-visible="7"
  prev-icon="mdi-chevron-left"
  next-icon="mdi-chevron-right"
></v-pagination>

分页与表格组件结合

v-data-table组件配合使用时,内置的分页功能更便捷。

<v-data-table
  :items="items"
  :headers="headers"
  :items-per-page="10"
  :page.sync="currentPage"
  hide-default-footer
>
  <template v-slot:footer>
    <v-pagination
      v-model="currentPage"
      :length="pageCount"
    ></v-pagination>
  </template>
</v-data-table>

分页器事件处理

监听分页变化事件,在页码改变时执行数据加载或其他操作。

watch: {
  currentPage(newVal) {
    this.fetchData()
    window.scrollTo(0, 0) // 返回页面顶部
  }
}

标签: 分页功能
分享给朋友:

相关文章

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…

vue组件传值实现分页

vue组件传值实现分页

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

php实现分页

php实现分页

PHP实现分页的基本方法 使用LIMIT和OFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。 $page = isset($_GET['page']) ? (int…

h5实现左右分页

h5实现左右分页

实现左右分页的HTML5方法 在HTML5中实现左右分页效果可以通过多种方式完成,以下是几种常见的方法: 使用CSS Flexbox布局 Flexbox是一种现代的布局方式,可以轻松实现左右分页效果…

vue实现功能插件

vue实现功能插件

Vue 功能插件实现方法 插件基本结构 Vue 插件通常需要暴露一个 install 方法,该方法接收 Vue 构造函数作为参数。基本结构如下: const MyPlugin = { insta…

vue 分页的实现

vue 分页的实现

分页组件实现 使用Vue实现分页功能通常需要结合后端API和前端组件。以下是一种常见的实现方式: 模板部分 <template> <div class="pagination…