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

在父组件中使用分页

在父组件中引入并使用分页组件,同时处理数据分页逻辑:

vue 实现简单分页

<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>

优化分页显示

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

vue 实现简单分页

// 在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 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑。…

实现简单vue

实现简单vue

实现一个简单的 Vue 框架 数据响应式系统 通过 Object.defineProperty 实现数据劫持,监听数据变化: function defineReactive(obj, key, va…

vue怎样实现分页

vue怎样实现分页

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

vue实现分页请求

vue实现分页请求

Vue 实现分页请求的方法 在 Vue 中实现分页请求通常需要结合前端分页组件和后端 API 的分页逻辑。以下是常见的实现方法: 分页组件与数据绑定 使用 v-pagination 或其他分页组件(…

vue实现简单拖拽

vue实现简单拖拽

Vue 实现简单拖拽功能 在 Vue 中实现拖拽功能可以通过 HTML5 的拖放 API 或第三方库(如 vuedraggable)来完成。以下是两种常见的实现方式: 使用 HTML5 拖放 API…

vue iview实现分页

vue iview实现分页

Vue + iView 实现分页 在 Vue 项目中结合 iView 组件库实现分页功能,可以通过 Page 组件轻松完成。以下是具体实现方法: 安装 iView 确保项目中已安装 iView,可以…