当前位置:首页 > VUE

用vue实现分页

2026-03-09 00:11:40VUE

使用 Vue 实现分页

分页组件的基本结构

创建一个独立的 Pagination.vue 组件,包含页码按钮、上一页和下一页按钮。通过 props 接收总页数、当前页等参数,通过 emit 触发页码变更事件。

用vue实现分页

<template>
  <div class="pagination">
    <button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">
      上一页
    </button>
    <button
      v-for="page in pages"
      :key="page"
      @click="changePage(page)"
      :class="{ active: page === currentPage }"
    >
      {{ page }}
    </button>
    <button
      @click="changePage(currentPage + 1)"
      :disabled="currentPage === totalPages"
    >
      下一页
    </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: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit("page-changed", page);
      }
    },
  },
};
</script>

<style>
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  cursor: pointer;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
</style>

在父组件中使用分页

在父组件中引入分页组件,传递总数据量、每页条数和当前页码,监听页码变化事件并更新数据。

用vue实现分页

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <Pagination
      :total-items="data.length"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from "./Pagination.vue";

export default {
  components: { Pagination },
  data() {
    return {
      data: [
        { id: 1, name: "Item 1" },
        { id: 2, name: "Item 2" },
        // 更多数据...
      ],
      itemsPerPage: 5,
      currentPage: 1,
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    },
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
    },
  },
};
</script>

优化分页显示

对于大量页码的情况,可以只显示当前页附近的页码,避免按钮过多。修改 pages 计算属性:

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;
},

添加省略号效果

进一步优化显示效果,在首尾页码不连续时显示省略号:

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

    <button v-if="showFirstEllipsis" disabled>...</button>
    <button
      v-for="page in pages"
      :key="page"
      @click="changePage(page)"
      :class="{ active: page === currentPage }"
    >
      {{ page }}
    </button>
    <button v-if="showLastEllipsis" disabled>...</button>

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

<script>
export default {
  computed: {
    showFirstEllipsis() {
      return this.pages[0] > 1;
    },
    showLastEllipsis() {
      return this.pages[this.pages.length - 1] < this.totalPages;
    },
  },
};
</script>

通过这些方法可以实现一个功能完善且用户友好的分页组件,适用于大多数 Vue 项目需求。

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

相关文章

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…