当前位置:首页 > VUE

vue实现分页页码

2026-02-23 05:04:44VUE

实现分页页码的基本思路

在Vue中实现分页页码功能,通常需要结合后端API返回的数据和前端的分页逻辑。核心是通过计算属性动态生成页码数组,并处理页码切换事件。

基础实现方案

模板部分

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>

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

    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

脚本部分

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    maxDisplayedPages: {
      type: Number,
      default: 5
    }
  },
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    displayedPages() {
      const pages = []
      const half = Math.floor(this.maxDisplayedPages / 2)
      let start = Math.max(1, this.currentPage - half)
      let end = Math.min(this.totalPages, start + this.maxDisplayedPages - 1)

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

      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      return pages
    }
  },
  methods: {
    changePage(page) {
      if (page !== this.currentPage) {
        this.currentPage = page
        this.$emit('page-changed', page)
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.changePage(this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.changePage(this.currentPage + 1)
      }
    }
  }
}
</script>

样式优化建议

<style scoped>
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: 20px 0;
}

.pagination button {
  padding: 5px 10px;
  cursor: pointer;
}

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

.pagination span {
  padding: 5px 10px;
  cursor: pointer;
  border-radius: 3px;
}

.pagination span:hover {
  background-color: #eee;
}

.pagination .active {
  background-color: #42b983;
  color: white;
}
</style>

高级功能扩展

省略号处理 当页码较多时,可以添加省略号表示隐藏的页码:

computed: {
  displayedPages() {
    const pages = []
    const maxVisible = this.maxDisplayedPages
    const total = this.totalPages

    if (total <= maxVisible) {
      for (let i = 1; i <= total; i++) pages.push(i)
      return pages
    }

    const left = Math.max(2, this.currentPage - 1)
    const right = Math.min(total - 1, this.currentPage + 1)

    pages.push(1)
    if (left > 2) pages.push('...')

    for (let i = left; i <= right; i++) pages.push(i)

    if (right < total - 1) pages.push('...')
    pages.push(total)

    return pages
  }
}

模板调整

<span 
  v-for="(page, index) in displayedPages" 
  :key="index"
  @click="typeof page === 'number' && changePage(page)"
  :class="{ 
    active: currentPage === page,
    ellipsis: page === '...'
  }"
>
  {{ page }}
</span>

与后端API集成

实际项目中通常需要从后端获取分页数据:

methods: {
  async fetchData(page = 1) {
    try {
      const response = await axios.get('/api/data', {
        params: {
          page,
          limit: this.itemsPerPage
        }
      })
      this.data = response.data.items
      this.totalItems = response.data.total
      this.currentPage = page
    } catch (error) {
      console.error(error)
    }
  }
}

使用第三方库

对于复杂需求,可以考虑使用现成的分页组件库:

  • v-pagination (Vue 2)
  • vue-awesome-paginate (Vue 3)

安装示例:

npm install vue-awesome-paginate

使用示例:

import VueAwesomePaginate from "vue-awesome-paginate";
import "vue-awesome-paginate/dist/style.css";

// Vue 3
app.use(VueAwesomePaginate);

// 模板中使用
<vue-awesome-paginate
  :total-items="totalItems"
  :items-per-page="itemsPerPage"
  v-model="currentPage"
/>

vue实现分页页码

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

相关文章

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子来…

vue实现小说分页

vue实现小说分页

实现小说分页功能 Vue.js可以通过计算属性、动态渲染和分页逻辑实现小说分页功能。以下是具体实现方法: 数据准备 需要将小说内容存储为字符串或数组格式,建议按章节拆分: data() { r…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…

vue分页插件实现

vue分页插件实现

Vue 分页插件实现方法 使用现成组件库(推荐) Element UI、Ant Design Vue 等主流库已提供成熟分页组件,可直接集成: <template> <el-…

vue实现搜索分页

vue实现搜索分页

实现搜索分页的基本思路 在Vue中实现搜索分页功能通常需要结合以下几个关键点:数据绑定、搜索过滤、分页逻辑处理。以下是一个完整的实现方案。 数据准备与绑定 定义数据列表和分页相关变量,通常包括当前页…

vue实现滚动分页

vue实现滚动分页

实现滚动分页的基本思路 滚动分页(Infinite Scroll)是一种常见的前端分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 结合现代前端工具可以轻松实现这一功能。 监听滚动事件…