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

样式优化建议

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

.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 分页的实现

分页组件实现 使用Vue实现分页功能通常需要结合后端API和前端组件。以下是一种常见的实现方式: 模板部分 <template> <div class="paginati…

jquery分页

jquery分页

jQuery分页的实现方法 jQuery分页可以通过多种方式实现,以下是几种常见的方法: 使用插件实现分页 jQuery有许多优秀的分页插件,如jQuery Pagination、DataTabl…

分页用vue实现

分页用vue实现

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

react如何实现分页

react如何实现分页

实现分页的基本思路 在React中实现分页通常需要以下几个核心步骤:管理当前页码状态、计算分页数据、渲染分页控件。以下是一个典型的分页实现方法。 管理分页状态 使用React的useState钩子…

vue 实现数据分页

vue 实现数据分页

实现数据分页的基本思路 在Vue中实现数据分页通常需要结合前端分页逻辑和后端API支持。前端分页适合数据量较小的情况,后端分页适合大数据量场景。 前端分页实现 创建一个计算属性来分割当前页数据:…

vue分页插件实现

vue分页插件实现

Vue 分页插件实现方法 使用现成组件库(推荐) Element UI、Ant Design Vue 等主流库已提供成熟分页组件,可直接集成: <template> <el-…