当前位置:首页 > VUE

vue实现分页组件

2026-02-17 09:51:57VUE

Vue 分页组件实现

基础分页组件代码

以下是一个基于 Vue 3 的分页组件实现,包含基本功能和样式:

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

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

    <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
    },
    maxVisibleButtons: {
      type: Number,
      default: 5
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    displayedPages() {
      const range = []
      const half = Math.floor(this.maxVisibleButtons / 2)
      let start = Math.max(this.currentPage - half, 1)
      let end = Math.min(start + this.maxVisibleButtons - 1, this.totalPages)

      if (end - start + 1 < this.maxVisibleButtons) {
        start = Math.max(end - this.maxVisibleButtons + 1, 1)
      }

      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>

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

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

.pagination button:hover:not(:disabled) {
  background: #eee;
}

.pagination button.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

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

组件使用示例

<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
      // 可选: 这里可以添加API调用获取新页数据
    }
  },
  async created() {
    // 获取初始数据
    // this.items = await fetchItems()
  }
}
</script>

高级功能扩展

  1. 添加省略号表示更多页码
<template>
  <div class="pagination">
    <button @click="changePage(1)" :disabled="currentPage === 1">首页</button>
    <button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>

    <template v-if="startPage > 1">
      <button @click="changePage(1)">1</button>
      <span v-if="startPage > 2">...</span>
    </template>

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

    <template v-if="endPage < totalPages">
      <span v-if="endPage < totalPages - 1">...</span>
      <button @click="changePage(totalPages)">{{ totalPages }}</button>
    </template>

    <button @click="changePage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
    <button @click="changePage(totalPages)" :disabled="currentPage === totalPages">末页</button>
  </div>
</template>
  1. 与API集成
methods: {
  async changePage(page) {
    if (page >= 1 && page <= this.totalPages) {
      this.currentPage = page
      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)
      }
    }
  }
}
  1. 响应式设计优化
@media (max-width: 600px) {
  .pagination {
    flex-wrap: wrap;
  }
  .pagination button {
    margin-bottom: 5px;
  }
  .pagination span {
    display: none;
  }
}

注意事项

vue实现分页组件

  • 确保在父组件中正确处理page-changed事件
  • 当数据量变化时,可能需要重置当前页码为1
  • 对于大型数据集,考虑使用服务器端分页而非客户端分页
  • 可以通过props传递自定义样式类名实现更灵活的样式控制

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

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

分页用vue实现

分页用vue实现

实现分页的基本思路 在Vue中实现分页功能,通常需要以下几个核心部分:数据列表、当前页码、每页显示数量、总页数计算、页码切换逻辑。以下是一个完整的实现方案。 分页组件代码示例 <templat…

vue 分页功能实现

vue 分页功能实现

分页功能实现方法 使用 Element UI 的分页组件 Element UI 提供了现成的分页组件 el-pagination,可以快速实现分页功能。 安装 Element UI: npm in…

vue 实现toast组件

vue 实现toast组件

实现 Vue Toast 组件的方法 创建基础 Toast 组件 在 Vue 项目中创建一个基础 Toast 组件,通常包含消息内容和显示控制逻辑。 <template> &…

vue 实现弹窗组件

vue 实现弹窗组件

Vue 弹窗组件实现 基础弹窗组件结构 创建一个名为Modal.vue的文件,定义弹窗的基础结构和样式: <template> <div class="modal-mask"…

vue 实现分页效果

vue 实现分页效果

使用 Element UI 实现分页 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI: npm install…