当前位置:首页 > 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 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API) UI组件…

vue搜索功能实现

vue搜索功能实现

Vue搜索功能实现方法 在Vue中实现搜索功能可以通过多种方式完成,以下是几种常见的方法: 使用计算属性实现搜索 计算属性非常适合处理需要根据输入值动态过滤数据的情况。创建一个计算属性,根据搜索关键…

vue实现数据渲染

vue实现数据渲染

Vue 数据渲染的实现方式 Vue.js 提供了多种数据渲染的方式,包括插值表达式、指令和计算属性等。以下是常见的实现方法: 插值表达式 使用双大括号 {{ }} 将数据绑定到模板中,数据变化时会自…