当前位置:首页 > VUE

vue实现翻页效果

2026-03-07 17:33:09VUE

实现基础翻页功能

使用Vue的v-for指令和计算属性实现数据分页展示。核心逻辑是通过计算属性paginatedData动态返回当前页的数据片段。

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>当前页: {{ currentPage }}</span>
    <button @click="nextPage" :disabled="currentPage >= pageCount">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [...], // 你的数据数组
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.items.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

添加页码导航器

扩展基础功能,显示可点击的页码按钮。使用v-for循环生成页码按钮,并通过计算属性控制显示范围。

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

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

    <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
    <button @click="goToPage(pageCount)" :disabled="currentPage === pageCount">末页</button>
  </div>
</template>

<script>
export default {
  computed: {
    displayedPages() {
      const range = 2 // 显示当前页前后各2页
      let start = Math.max(1, this.currentPage - range)
      let end = Math.min(this.pageCount, this.currentPage + range)

      if (this.currentPage - start < range) {
        end = Math.min(this.pageCount, end + (range - (this.currentPage - start)))
      }
      if (end - this.currentPage < range) {
        start = Math.max(1, start - (range - (end - this.currentPage)))
      }

      const pages = []
      for (let i = start; i <= end; i++) {
        pages.push(i)
      }
      return pages
    }
  },
  methods: {
    goToPage(page) {
      this.currentPage = page
    }
  }
}
</script>

<style>
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

使用第三方分页组件

对于更复杂的需求,可以考虑使用现成的Vue分页组件如vue-paginateelement-ui的分页组件。

安装element-ui分页组件:

npm install element-ui

使用示例:

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 30, 40]"
    :page-size="itemsPerPage"
    layout="total, sizes, prev, pager, next, jumper"
    :total="totalItems">
  </el-pagination>
</template>

<script>
import { ElPagination } from 'element-ui'

export default {
  components: {
    ElPagination
  },
  methods: {
    handleSizeChange(val) {
      this.itemsPerPage = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

服务器端分页实现

当数据量很大时,需要结合API实现服务器端分页。通过axios发送分页参数到后端,接收分页结果。

<script>
import axios from 'axios'

export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      itemsPerPage: 10,
      totalItems: 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      axios.get('/api/items', {
        params: {
          page: this.currentPage,
          size: this.itemsPerPage
        }
      }).then(response => {
        this.items = response.data.items
        this.totalItems = response.data.total
      })
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}
</script>

分页性能优化

对于前端分页大数据量场景,可以采用虚拟滚动技术优化性能。使用vue-virtual-scroller等库实现。

安装:

npm install vue-virtual-scroller

实现示例:

vue实现翻页效果

<template>
  <RecycleScroller
    class="scroller"
    :items="paginatedData"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="item">{{ item.name }}</div>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: {
    RecycleScroller
  }
}
</script>

<style>
.scroller {
  height: 500px;
}
</style>

标签: 翻页效果
分享给朋友:

相关文章

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…

vue实现抽屉效果

vue实现抽屉效果

Vue 实现抽屉效果的方法 使用 Vue 过渡动画和 CSS 在 Vue 中可以通过 v-if 或 v-show 控制抽屉的显示隐藏,结合 <transition> 组件实现平滑动画效果。…

vue实现滑动效果

vue实现滑动效果

Vue 实现滑动效果的方法 在 Vue 中实现滑动效果可以通过多种方式,包括使用 CSS 过渡、第三方动画库或直接操作 DOM。以下是几种常见的方法: 使用 CSS 过渡和 Vue 的 <tr…

vue实现tab效果

vue实现tab效果

Vue 实现 Tab 效果的方法 使用动态组件和 v-if 通过 v-if 或 v-show 控制不同标签内容的显示与隐藏,结合点击事件切换当前激活的标签页。 <template>…

vue实现数字效果

vue实现数字效果

Vue 实现数字动画效果 在 Vue 中实现数字动画效果通常涉及数字的递增、滚动或变化动画。以下是几种常见实现方式: 使用第三方库(如 vue-count-to) 安装 vue-count-to 库…