当前位置:首页 > VUE

如何用vue实现分页

2026-02-22 16:29:22VUE

实现分页的基本思路

在Vue中实现分页功能通常需要结合后端API返回的数据和前端的分页逻辑。分页的核心是计算当前页的数据范围,并根据总数据量生成分页按钮。

后端API分页参数

大多数后端API会接受分页参数,例如page(当前页码)和pageSize(每页条数)。API返回的数据通常包含items(当前页数据)和total(总数据量)。

// 示例API请求参数
const params = {
  page: 1,
  pageSize: 10
}

前端分页组件

使用Vue实现分页时,可以创建一个分页组件,接收currentPagetotalItemsitemsPerPage作为props,并发出page-changed事件。

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

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

    <button
      @click="changePage(currentPage + 1)"
      :disabled="currentPage === totalPages"
    >
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: {
      type: Number,
      required: true
    },
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    }
  },
  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>

<style>
.pagination button {
  margin: 0 5px;
}
.pagination button.active {
  font-weight: bold;
  color: blue;
}
</style>

在父组件中使用分页

父组件需要管理当前页码,并在页码变化时重新获取数据。

<template>
  <div>
    <table>
      <!-- 显示当前页数据 -->
      <tr v-for="item in currentItems" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>

    <pagination
      :current-page="currentPage"
      :total-items="totalItems"
      :items-per-page="itemsPerPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

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

export default {
  components: {
    Pagination
  },
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 10,
      totalItems: 0,
      currentItems: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // 模拟API调用
      const params = {
        page: this.currentPage,
        pageSize: this.itemsPerPage
      }

      // 实际项目中这里应该是axios调用
      mockApi(params).then(response => {
        this.currentItems = response.items
        this.totalItems = response.total
      })
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}

// 模拟API函数
function mockApi(params) {
  const allItems = Array.from({length: 100}, (_, i) => ({id: i+1, name: `Item ${i+1}`}))
  const start = (params.page - 1) * params.pageSize
  const end = start + params.pageSize
  return Promise.resolve({
    items: allItems.slice(start, end),
    total: allItems.length
  })
}
</script>

前端分页实现

如果数据量不大,也可以在前端实现完整分页:

computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return this.allItems.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.allItems.length / this.itemsPerPage)
  }
}

分页优化技巧

对于大量数据的分页,可以限制显示的页码按钮数量,只显示当前页附近的几个页码:

pages() {
  const range = []
  const maxVisible = 5 // 最多显示5个页码
  let start = Math.max(1, this.currentPage - Math.floor(maxVisible / 2))
  let end = Math.min(this.totalPages, start + maxVisible - 1)

  if (end - start + 1 < maxVisible) {
    start = Math.max(1, end - maxVisible + 1)
  }

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

  return range
}

分页样式美化

可以使用CSS美化分页组件,添加过渡效果:

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

.pagination button {
  padding: 5px 10px;
  margin: 0 2px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination button:hover {
  background: #eee;
}

.pagination button.active {
  background: #4285f4;
  color: white;
  border-color: #4285f4;
}

.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

如何用vue实现分页

标签: 分页如何用
分享给朋友:

相关文章

php实现的分页

php实现的分页

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

jquery 分页

jquery 分页

实现 jQuery 分页的方法 客户端分页实现 使用 jQuery 实现客户端分页的核心思路是通过 JavaScript 控制数据的显示与隐藏。假设有一个包含数据的 HTML 列表或表格: //…

如何用css制作网页

如何用css制作网页

使用CSS制作网页的基本方法 CSS(层叠样式表)用于控制网页的样式和布局。以下是通过CSS设计和美化网页的核心方法: 链接CSS文件 在HTML文件的<head>部分添加外部CSS文…

vue加载实现分页

vue加载实现分页

Vue 实现分页加载的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用其分页组件。在模板中添加分页组件,并绑定相关事件和数据。 <template&g…

vue分页插件实现

vue分页插件实现

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

vue实现前端分页

vue实现前端分页

实现前端分页的方法 在Vue中实现前端分页通常需要结合数据截取和分页组件。以下是常见的实现方式: 基础实现方案 数据准备 定义总数据数组和分页相关变量: data() { return {…