…">
当前位置:首页 > VUE

vue实现分页页面

2026-02-24 02:08:23VUE

Vue 分页实现方法

基础分页组件实现

创建一个可复用的分页组件 Pagination.vue

<template>
  <div class="pagination">
    <button 
      @click="changePage(currentPage - 1)" 
      :disabled="currentPage === 1"
    >
      上一页
    </button>

    <span v-for="page in pages" :key="page">
      <button 
        @click="changePage(page)"
        :class="{ active: currentPage === page }"
      >
        {{ page }}
      </button>
    </span>

    <button 
      @click="changePage(currentPage + 1)" 
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page)
      }
    }
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

在父组件中使用分页

<template>
  <div>
    <!-- 数据列表 -->
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <!-- 分页组件 -->
    <Pagination
      :total-items="data.length"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      data: [], // 从API获取的数据
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.data.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
    },
    fetchData() {
      // API请求获取数据
      // this.data = response.data
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

高级分页功能实现

对于大量数据的分页显示,可以添加页码省略功能:

vue实现分页页面

// 在Pagination组件的computed中替换pages计算属性
pages() {
  const range = []
  const current = this.currentPage
  const total = this.totalPages
  const delta = 2 // 显示当前页前后多少页

  range.push(1)
  if (current - delta > 2) {
    range.push('...')
  }

  for (let i = Math.max(2, current - delta); i <= Math.min(total - 1, current + delta); i++) {
    range.push(i)
  }

  if (current + delta < total - 1) {
    range.push('...')
  }
  if (total > 1) {
    range.push(total)
  }

  return range
}

与后端API集成

当需要从后端获取分页数据时:

methods: {
  async fetchData() {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          per_page: this.itemsPerPage
        }
      })
      this.data = response.data.items
      this.totalItems = response.data.total_count
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData()
  }
}

使用第三方库

可以考虑使用现成的分页组件库:

vue实现分页页面

  • v-pagination (轻量级)
  • element-uiel-pagination
  • vuetifyv-pagination

安装示例(使用v-pagination):

npm install v-pagination

使用方式:

<template>
  <pagination
    v-model="currentPage"
    :records="totalItems"
    :per-page="itemsPerPage"
    @paginate="fetchData"
  />
</template>

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

相关文章

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

jquery页面

jquery页面

jQuery 页面操作指南 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互的操作。以下是 jQuery 在页面中的常见使用方法…

h5实现页面切换

h5实现页面切换

h5实现页面切换的方法 在H5中实现页面切换可以通过多种方式完成,包括使用原生HTML链接、JavaScript动态加载、框架路由等。以下是几种常见的方法: 使用原生HTML的<a>标签…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 安装 Vue.js 依赖 通过 npm 或 yarn 安装 Vue.js: npm install vue # 或 yarn add vue 创建 Vue 实例 在…

vue实现页面手写

vue实现页面手写

Vue 实现手写功能 在 Vue 中实现手写功能通常需要使用 HTML5 的 Canvas 元素,结合鼠标或触摸事件来捕捉用户的绘制动作。以下是实现步骤和代码示例: 基础实现步骤 创建一个 Vue…

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 ro…