当前位置:首页 > VUE

vue实现分页组件

2026-01-16 17:13:32VUE

Vue 分页组件实现

基础分页组件结构

创建一个名为 Pagination.vue 的文件,包含以下基础模板和逻辑:

vue实现分页组件

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

<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 = []
      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 scoped>
.pagination {
  display: flex;
  gap: 8px;
}
.active {
  font-weight: bold;
  color: blue;
}
</style>

优化分页显示逻辑

当页数较多时,可以只显示部分页码,添加以下计算属性替换原来的 pages

vue实现分页组件

pages() {
  const range = []
  const maxVisible = 5 // 最多显示5个页码
  let start = 1
  let end = this.totalPages

  if (this.totalPages > maxVisible) {
    start = Math.max(this.currentPage - 2, 1)
    end = Math.min(this.currentPage + 2, this.totalPages)

    if (this.currentPage <= 3) {
      end = maxVisible
    } else if (this.currentPage >= this.totalPages - 2) {
      start = this.totalPages - maxVisible + 1
    }
  }

  for (let i = start; i <= end; i++) {
    range.push(i)
  }

  return range
}

在父组件中使用

<template>
  <div>
    <!-- 列表数据展示 -->
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <!-- 分页组件 -->
    <Pagination
      :total-items="items.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 {
      items: [], // 从API获取的数据
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      // 可以在这里添加获取新页数据的逻辑
    }
  }
}
</script>

添加省略号功能

修改分页组件模板,添加省略号显示:

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

    <span v-if="showStartEllipsis">...</span>

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

    <span v-if="showEndEllipsis">...</span>

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

<script>
export default {
  // ...其他代码不变...
  computed: {
    showStartEllipsis() {
      return this.pages.length > 0 && this.pages[0] > 1
    },
    showEndEllipsis() {
      return this.pages.length > 0 && 
             this.pages[this.pages.length - 1] < this.totalPages
    }
  }
}
</script>

响应式设计考虑

添加媒体查询适应移动设备:

@media (max-width: 600px) {
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }
  .pagination button, .pagination span {
    margin: 4px;
    padding: 4px 8px;
  }
}

标签: 分页组件
分享给朋友:

相关文章

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template…

vue实现组件封装

vue实现组件封装

Vue 组件封装方法 封装组件的基本步骤 创建独立的 .vue 文件,包含 template、script 和 style 三个部分。通过 props 接收父组件传递的数据,使用 $emit 触发…

vue elementui实现分页

vue elementui实现分页

使用 Element UI 实现 Vue 分页功能 Element UI 提供了 el-pagination 组件,可以方便地实现分页功能。以下是具体实现步骤: 安装 Element UI 确保项目…