当前位置:首页 > 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 分页的实现方法 使用 Element UI 的分页组件 安装 Element UI: npm install element-ui 在 Vue 项目中引入 Element UI 的分页组件:…

vue怎么实现组件缓存

vue怎么实现组件缓存

vue实现组件缓存的方法 在Vue中实现组件缓存可以通过内置的<keep-alive>组件完成,该组件能够缓存不活动的组件实例,避免重复渲染。 使用<keep-alive>…

vue实现选项卡分页

vue实现选项卡分页

实现选项卡分页的基本思路 在Vue中实现选项卡分页功能,可以通过动态组件或条件渲染结合数据绑定完成。核心逻辑是维护一个当前激活的选项卡状态,根据用户点击切换内容。 使用v-if条件渲染 通过v-i…

vue组件实现

vue组件实现

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

vue router实现分页

vue router实现分页

使用 Vue Router 实现分页 在 Vue.js 项目中,可以通过 Vue Router 结合分页组件实现分页功能。以下是实现分页的几种方法: 使用路由参数传递页码 在路由配置中定义一个动态…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class…