当前位置:首页 > VUE

vue分页插件实现分页

2026-02-22 22:55:45VUE

Vue 分页插件实现方法

使用第三方库(如 vue-paginate

安装 vue-paginate 库:

npm install vue-paginate --save

注册插件并实现分页:

// 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>

自定义分页组件

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

<!-- 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 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

php分页代码简单实现

php分页代码简单实现

基本分页实现 在PHP中实现分页功能通常需要结合MySQL的LIMIT子句。以下是一个基础实现示例: <?php // 数据库连接 $conn = mysqli_connect("localh…

php实现分页功能

php实现分页功能

PHP实现分页功能的方法 数据库查询与分页逻辑 使用SQL的LIMIT和OFFSET实现分页。假设每页显示10条数据,当前页码为$page: $perPage = 10; $page = isset…

h5实现左右分页

h5实现左右分页

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

vue 分页的实现

vue 分页的实现

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

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…