当前位置:首页 > VUE

vue分页插件实现分页

2026-02-22 22:55:45VUE

Vue 分页插件实现方法

使用第三方库(如 vue-paginate

安装 vue-paginate 库:

npm install vue-paginate --save

注册插件并实现分页:

vue分页插件实现分页

// main.js
import VuePaginate from 'vue-paginate'
Vue.use(VuePaginate)

// 组件内
<template>
  <paginate
    :page-count="totalPages"
    :click-handler="handlePageChange"
    :prev-text="'上一页'"
    :next-text="'下一页'"
  ></paginate>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      totalItems: 100
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    }
  },
  methods: {
    handlePageChange(pageNum) {
      this.currentPage = pageNum
      this.fetchData()
    },
    fetchData() {
      // 根据 currentPage 获取数据
    }
  }
}
</script>

自定义分页组件

创建一个可复用的分页组件:

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: {
    currentPage: Number,
    totalPages: Number,
    maxVisibleButtons: {
      type: Number,
      default: 5
    }
  },
  computed: {
    pages() {
      const range = []
      const half = Math.floor(this.maxVisibleButtons / 2)
      let start = Math.max(1, this.currentPage - half)
      let end = Math.min(this.totalPages, start + this.maxVisibleButtons - 1)

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

      for (let i = start; i <= end; 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>

后端分页结合 API

与后端 API 交互实现分页:

// 在父组件中使用
<template>
  <div>
    <data-table :items="items" />
    <pagination 
      :current-page="currentPage"
      :total-pages="totalPages"
      @page-changed="onPageChange"
    />
  </div>
</template>

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

export default {
  components: { Pagination, DataTable },
  data() {
    return {
      items: [],
      currentPage: 1,
      totalPages: 0,
      totalItems: 0
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const response = await axios.get('/api/items', {
        params: {
          page: this.currentPage,
          per_page: 10
        }
      })
      this.items = response.data.data
      this.totalItems = response.data.total
      this.totalPages = Math.ceil(this.totalItems / 10)
    },
    onPageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}
</script>

分页样式优化

添加过渡效果和响应式设计:

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

.pagination button {
  transition: all 0.3s ease;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}

.pagination button:hover:not(.active) {
  background: #f1f1f1;
}

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

@media (max-width: 600px) {
  .pagination button {
    padding: 3px 6px;
    margin: 0 2px;
  }
}

这些方法提供了从简单到复杂的分页实现方案,可根据项目需求选择合适的方式。第三方库适合快速实现,自定义组件灵活性更高,后端分页适合大数据量场景。

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

相关文章

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现插件

vue实现插件

Vue 插件的基本实现 Vue 插件是一种扩展 Vue 功能的机制,通常用于添加全局功能或组件。插件可以是一个对象或函数,需要提供 install 方法。 const MyPlugin = {…

vue插件实现联动

vue插件实现联动

Vue 插件实现联动的方法 使用全局事件总线 在 Vue 中可以通过创建一个全局事件总线来实现组件间的联动。首先创建一个新的 Vue 实例作为事件总线。 // event-bus.js import…

jquery插件下载

jquery插件下载

jQuery插件下载方法 访问jQuery官方网站(jquery.com)下载最新版本的jQuery库。官网提供压缩版(minified)和未压缩版(uncompressed)两种格式,适用于不同开发…

vue中实现分页

vue中实现分页

vue中实现分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,适合快速集成。需先安装Element UI库。 安装命令: npm…

vue实现分页加载

vue实现分页加载

Vue 分页加载的实现方法 基础分页实现(后端分页) 安装依赖(如使用axios): npm install axios 示例代码: <template> <div>…