当前位置:首页 > VUE

vue分页插件实现

2026-02-17 09:33:26VUE

Vue 分页插件实现方法

使用第三方库(推荐)

安装 vuejs-paginatev-pagination 等成熟分页库:

npm install vuejs-paginate

注册组件:

import Vue from 'vue'
import VuePaginate from 'vuejs-paginate'
Vue.component('paginate', VuePaginate)

模板中使用:

vue分页插件实现

<paginate
  :page-count="totalPages"
  :click-handler="changePage"
  :prev-text="'Prev'"
  :next-text="'Next'"
  :container-class="'pagination'"
/>

自定义分页组件

创建 Pagination.vue 组件:

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="goToPage(page)"
        :class="{ active: currentPage === page }"
      >{{ page }}</button>
    </span>
    <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
  },
  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: {
    prevPage() {
      this.$emit('page-changed', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-changed', this.currentPage + 1)
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

与数据结合使用

父组件中实现分页逻辑:

vue分页插件实现

data() {
  return {
    currentPage: 1,
    itemsPerPage: 10,
    allItems: [...] // 你的完整数据
  }
},
computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return this.allItems.slice(start, end)
  }
},
methods: {
  handlePageChange(page) {
    this.currentPage = page
  }
}

样式定制

添加基础CSS样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.pagination button.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

高级功能扩展

对于大型数据集:

  • 添加页面跳转输入框
  • 实现动态页码范围计算(只显示当前页附近的页码)
  • 添加每页条数选择器
  • 结合路由实现URL同步
// 动态页码计算示例
pages() {
  const range = []
  const maxVisible = 5
  let start = Math.max(1, this.currentPage - Math.floor(maxVisible/2))
  let end = Math.min(this.totalPages, start + maxVisible - 1)

  if (end - start + 1 < maxVisible) {
    start = Math.max(1, end - maxVisible + 1)
  }

  for (let i = start; i <= end; i++) {
    range.push(i)
  }

  return range
}

标签: 分页插件
分享给朋友:

相关文章

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue 实现分页

vue 实现分页

分页实现方法 在Vue中实现分页功能,可以通过计算属性和方法结合来实现。以下是一个典型的分页实现方式: 模板部分 <template> <div> <…

h5实现左右分页

h5实现左右分页

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

vue插件实现原理

vue插件实现原理

Vue 插件实现原理 Vue 插件的核心是通过暴露一个 install 方法,在 Vue 应用初始化时被调用,从而扩展 Vue 的功能。插件可以添加全局方法、指令、混入、组件等。 插件基本结构 一个…

vue前端实现分页

vue前端实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。核心逻辑包括计算总页数、处理当前页码变化以及动态加载数据。 使用Element UI的分页组件 Element…

分页用vue实现

分页用vue实现

实现分页的基本思路 在Vue中实现分页功能,通常需要以下几个核心部分:数据列表、当前页码、每页显示数量、总页数计算、页码切换逻辑。以下是一个完整的实现方案。 分页组件代码示例 <templa…