当前位置:首页 > VUE

分页用vue实现

2026-01-15 04:41:07VUE

实现分页的基本思路

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

分页组件代码示例

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span v-for="page in totalPages" :key="page">
        <button 
          @click="goToPage(page)"
          :class="{ active: currentPage === page }"
        >
          {{ page }}
        </button>
      </span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataList: [], // 原始数据
      currentPage: 1, // 当前页码
      itemsPerPage: 10 // 每页显示数量
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.dataList.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.dataList.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    goToPage(page) {
      this.currentPage = page
    }
  },
  created() {
    // 模拟数据获取
    for (let i = 1; i <= 100; i++) {
      this.dataList.push({ id: i, name: `项目 ${i}` })
    }
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

关键实现细节

数据分片计算 通过计算属性paginatedData实现当前页数据的切片显示:

paginatedData() {
  const start = (this.currentPage - 1) * this.itemsPerPage
  const end = start + this.itemsPerPage
  return this.dataList.slice(start, end)
}

总页数计算 基于数据总量和每页显示数量计算总页数:

分页用vue实现

totalPages() {
  return Math.ceil(this.dataList.length / this.itemsPerPage)
}

页码切换控制 提供三个基本方法处理页码切换:

prevPage() {
  if (this.currentPage > 1) {
    this.currentPage--
  }
},
nextPage() {
  if (this.currentPage < this.totalPages) {
    this.currentPage++
  }
},
goToPage(page) {
  this.currentPage = page
}

进阶优化建议

分页组件封装 将分页逻辑抽离为独立组件,通过props接收数据:

分页用vue实现

<pagination 
  :total-items="dataList.length" 
  :items-per-page="itemsPerPage"
  :current-page="currentPage"
  @page-changed="handlePageChange"
/>

异步数据加载 结合API请求实现服务端分页:

async fetchData(page) {
  const response = await axios.get(`/api/items?page=${page}`)
  this.dataList = response.data.items
  this.totalItems = response.data.total
}

显示优化 添加页码范围限制,避免显示过多页码按钮:

visiblePages() {
  const range = 2
  const start = Math.max(1, this.currentPage - range)
  const end = Math.min(this.totalPages, this.currentPage + range)
  return Array.from({ length: end - start + 1 }, (_, i) => start + i)
}

以上实现方案涵盖了Vue分页的核心功能,可根据实际需求进行扩展和调整。

标签: 分页vue
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const tr…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mi…