当前位置:首页 > VUE

vue 分页的实现

2026-03-08 00:48:39VUE

分页组件的实现

在Vue中实现分页功能,通常需要结合后端API返回的分页数据。以下是一个常见的分页组件实现方式。

模板部分

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

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

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

脚本部分

vue 分页的实现

<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 = []
      const start = Math.max(1, this.currentPage - 2)
      const end = Math.min(this.totalPages, this.currentPage + 2)

      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>

分页数据获取

在实际应用中,通常需要从API获取分页数据。以下是一个示例:

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

分页样式

为分页组件添加基本样式:

vue 分页的实现

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

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

.pagination span.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
</style>

分页逻辑优化

对于大型数据集,可以考虑添加以下优化:

  • 添加页面跳转输入框
  • 显示总页数和当前页码
  • 添加首尾页跳转按钮
  • 实现动态页码范围计算
computed: {
  showStartEllipsis() {
    return this.currentPage > 3
  },
  showEndEllipsis() {
    return this.currentPage < this.totalPages - 2
  }
}

第三方分页组件

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

  • vue-paginate
  • v-pagination
  • element-uiel-pagination
  • vuetifyv-pagination

这些组件提供了更丰富的功能和更好的可定制性。

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

相关文章

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现type切换

vue实现type切换

Vue 实现 Type 切换的实现方法 在 Vue 中实现 Type 切换功能可以通过动态组件、条件渲染或路由切换等方式实现。以下是几种常见方法: 使用 v-if 或 v-show 条件渲染 通过绑…

vue实现子表格

vue实现子表格

Vue 实现子表格的方法 在 Vue 中实现子表格可以通过多种方式完成,以下是几种常见的方法: 使用嵌套组件 创建一个父表格组件和一个子表格组件,通过 props 将数据传递给子表格组件。父表格的每…

vue计算属性 实现

vue计算属性 实现

Vue 计算属性的实现 计算属性(Computed Properties)是 Vue 中的一个核心功能,用于声明式地定义依赖其他属性的值。计算属性会缓存结果,只有当依赖的响应式数据发生变化时才会重新计…

vue实现列表循环

vue实现列表循环

Vue 列表循环的实现方法 在 Vue 中,可以通过 v-for 指令实现列表循环渲染。以下是几种常见的实现方式: 基础列表渲染 <template> <ul>…

vue滚动插件实现

vue滚动插件实现

Vue 滚动插件实现方法 使用现有插件(推荐) 对于大多数场景,推荐使用成熟的 Vue 滚动插件,例如 vue-infinite-loading 或 vue-virtual-scroller。这些插件…