当前位置:首页 > VUE

vue 实现简单分页

2026-01-18 14:00:07VUE

Vue 实现简单分页的方法

基础分页组件实现

创建一个基础分页组件,包含页码按钮和翻页功能。以下是一个基于 Vue 3 的示例:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1"
    >
      上一页
    </button>

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

    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    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) {
      this.$emit('page-changed', page)
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.$emit('page-changed', this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.$emit('page-changed', this.currentPage + 1)
      }
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  gap: 8px;
}
.pagination span {
  cursor: pointer;
  padding: 4px 8px;
}
.pagination span.active {
  font-weight: bold;
  color: blue;
}
</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: 5,
      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>

优化分页显示

对于大量数据,可以优化页码显示,只显示当前页附近的页码:

// 在Pagination组件的computed中替换pages计算属性
pages() {
  const range = []
  const maxVisible = 5 // 最多显示5个页码
  let start = 1
  let end = this.totalPages

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

    if (this.currentPage <= 3) {
      end = maxVisible
    }

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

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

  return range
}

与API集成

当数据来自后端API时,可以在分页变化时发送请求:

methods: {
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData()
  },
  async fetchData() {
    const response = await axios.get('/api/items', {
      params: {
        page: this.currentPage,
        per_page: this.itemsPerPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.total_count
  }
}

样式优化建议

为分页组件添加更多样式选项,使其更美观:

vue 实现简单分页

.pagination {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-top: 20px;
}
.pagination button {
  padding: 6px 12px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}
.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
.pagination span {
  padding: 6px 12px;
  border: 1px solid #ddd;
  cursor: pointer;
}
.pagination span.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

这些方法提供了从基础到进阶的Vue分页实现方案,可以根据项目需求选择合适的实现方式。

标签: 分页简单
分享给朋友:

相关文章

vue  实现分页

vue 实现分页

实现分页功能 在Vue中实现分页功能通常需要结合后端API或前端数据处理。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,所有数据一次性加载到前端后进行分页处理: <…

vue实现分页打印

vue实现分页打印

实现分页打印的基本思路 在Vue中实现分页打印功能通常需要结合CSS的分页控制属性和JavaScript的打印API。核心是通过CSS的@media print媒体查询和page-break相关属性控…

vue搜索分页实现

vue搜索分页实现

Vue 搜索分页实现方法 基本思路 搜索分页通常需要结合后端API,前端通过Vue组件实现搜索框、分页器和数据列表的联动。核心逻辑包括:监听搜索输入变化、分页参数变化,触发数据请求并更新视图。 实现…

vue打印实现分页

vue打印实现分页

在Vue中实现打印分页功能 使用CSS分页控制 通过CSS的page-break属性控制打印时的分页行为。在需要分页的元素上添加以下样式类: .page-break { page-break-a…

vue分页前台实现

vue分页前台实现

Vue 分页前台实现 在 Vue 中实现分页功能,通常需要结合后端 API 返回的数据和前端分页组件。以下是几种常见的实现方式: 使用 Element UI 分页组件 Element UI 提供了现…

vue实现分页查询

vue实现分页查询

Vue 实现分页查询 基本思路 在 Vue 中实现分页查询通常需要结合后端 API 和前端组件。核心逻辑包括: 通过 API 获取分页数据 处理分页参数(当前页、每页条数) 渲染分页控件 监听页码变…