当前位置:首页 > VUE

vue分页组件的实现

2026-02-22 04:13:33VUE

基础分页组件实现

创建一个基本的Vue分页组件需要定义当前页码、总页数等核心属性,并通过事件触发页码切换。

模板部分

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

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

高级分页功能

对于数据量大的情况,需要优化分页显示逻辑,避免显示过多页码按钮。

计算属性优化

computed: {
  pages() {
    const current = this.currentPage
    const total = this.totalPages
    const range = 2 // 显示当前页前后各2页

    if (total <= 5) {
      return Array.from({ length: total }, (_, i) => i + 1)
    }

    let pages = []
    pages.push(1)

    if (current - range > 2) {
      pages.push('...')
    }

    const start = Math.max(2, current - range)
    const end = Math.min(total - 1, current + range)

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

    if (current + range < total - 1) {
      pages.push('...')
    }

    pages.push(total)
    return pages
  }
}

与表格数据结合

分页组件通常需要与表格数据配合使用,下面展示如何在实际场景中集成。

vue分页组件的实现

父组件使用示例

<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <pagination
      :total-items="totalItems"
      :items-per-page="perPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      currentPage: 1,
      perPage: 10,
      totalItems: 0,
      items: []
    }
  },
  methods: {
    async fetchData(page = 1) {
      const response = await api.getItems({
        page,
        limit: this.perPage
      })
      this.items = response.data
      this.totalItems = response.total
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData(page)
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

样式优化

为分页组件添加基础样式提升用户体验。

CSS样式

vue分页组件的实现

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

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

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

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

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

服务器端分页处理

当数据量极大时,需要实现服务器端分页以减少前端压力。

API请求示例

async fetchData(page = 1) {
  try {
    const response = await axios.get('/api/items', {
      params: {
        page,
        limit: this.perPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.totalCount
  } catch (error) {
    console.error('获取数据失败:', error)
  }
}

性能优化技巧

对于大型数据集,可以采用虚拟滚动或懒加载等技术提升性能。

虚拟滚动实现思路

// 使用vue-virtual-scroller等库
<template>
  <RecycleScroller
    :items="visibleItems"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <!-- 渲染单条数据 -->
    </template>
  </RecycleScroller>
  <pagination 
    :total-items="totalItems"
    @page-changed="handlePageChange"
  />
</template>

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

相关文章

如何设计react组件

如何设计react组件

设计 React 组件的核心原则 React 组件的设计需要遵循高内聚、低耦合的原则,确保组件功能独立且易于维护。组件的设计可以分为展示组件和容器组件两类,展示组件负责 UI 渲染,容器组件负责逻辑处…

php实现的分页

php实现的分页

PHP 实现分页的方法 基本分页逻辑 分页的核心在于计算总页数、当前页码以及数据偏移量。假设每页显示 10 条数据: $current_page = isset($_GET['page']) ? (…

组件elementui

组件elementui

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如按钮、表单、表格等),适合快速构建企业级中后台产品。…

vue动态组件实现

vue动态组件实现

动态组件的基本用法 在Vue中,动态组件通过<component>标签和is属性实现。is属性可以绑定组件名称或组件选项对象,实现动态切换。 <template> &l…

vue实现tooltips组件

vue实现tooltips组件

实现基础 Tooltip 组件 创建一个名为 Tooltip.vue 的单文件组件,包含模板、样式和逻辑: <template> <div class="tooltip-co…

vue elementui实现分页

vue elementui实现分页

Vue + ElementUI 分页实现方法 安装与引入 确保已安装 ElementUI,可通过 npm 或 yarn 安装: npm install element-ui 在 main.js 中…