当前位置:首页 > 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>

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

相关文章

php实现分页功能

php实现分页功能

PHP实现分页功能的方法 数据库查询与分页逻辑 使用SQL的LIMIT和OFFSET实现分页。假设每页显示10条数据,当前页码为$page: $perPage = 10; $page = isset…

uniapp 分页组件

uniapp 分页组件

uniapp 分页组件实现方法 在uniapp中实现分页功能,可以通过自定义组件或使用第三方组件库完成。以下是几种常见实现方式: 自定义分页组件 创建一个名为uni-pagination的组件,模板…

分页用vue实现

分页用vue实现

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

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref="…

如何判断react组件

如何判断react组件

判断 React 组件的依据 React 组件的判断通常基于其定义方式、功能特性和代码结构。以下是几种常见的判断方法: 函数组件与类组件 函数组件是通过 JavaScript 函数定义的,接收 pr…

vue怎样实现分页

vue怎样实现分页

Vue实现分页的方法 使用第三方库(如Element UI或Ant Design Vue) Element UI的Pagination组件可以直接集成到Vue项目中。安装Element UI后,在组件…