当前位置:首页 > VUE

vue实现翻页

2026-03-07 11:54:28VUE

Vue实现翻页功能

翻页功能通常用于分页展示数据,以下是几种常见的实现方式。

基础分页组件实现

创建一个简单的分页组件,包含上一页、下一页和页码按钮。

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

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    }
  },
  data() {
    return {
      currentPage: 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: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.$emit('page-changed', this.currentPage)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.$emit('page-changed', this.currentPage)
      }
    },
    goToPage(page) {
      this.currentPage = page
      this.$emit('page-changed', this.currentPage)
    }
  }
}
</script>

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

使用第三方库

对于更复杂的分页需求,可以使用现成的分页库如vuejs-paginate

安装库:

npm install vuejs-paginate

使用示例:

<template>
  <paginate
    :page-count="totalPages"
    :click-handler="goToPage"
    :prev-text="'上一页'"
    :next-text="'下一页'"
    :container-class="'pagination'"
    :page-class="'page-item'"
  >
  </paginate>
</template>

<script>
import Paginate from 'vuejs-paginate'

export default {
  components: {
    Paginate
  },
  props: {
    totalItems: Number,
    itemsPerPage: {
      type: Number,
      default: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    }
  },
  methods: {
    goToPage(pageNum) {
      this.$emit('page-changed', pageNum)
    }
  }
}
</script>

无限滚动翻页

对于移动端或需要流畅体验的场景,可以实现无限滚动翻页。

<template>
  <div class="infinite-scroll" @scroll="handleScroll">
    <div v-for="item in visibleItems" :key="item.id">
      {{ item.content }}
    </div>
    <div v-if="loading" class="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [], // 所有数据
      visibleItems: [], // 当前显示的数据
      pageSize: 10,
      currentPage: 1,
      loading: false
    }
  },
  mounted() {
    this.loadItems()
  },
  methods: {
    async loadItems() {
      this.loading = true
      // 模拟API调用
      const newItems = await this.fetchData(this.currentPage, this.pageSize)
      this.allItems = [...this.allItems, ...newItems]
      this.visibleItems = this.allItems.slice(0, this.currentPage * this.pageSize)
      this.loading = false
    },
    fetchData(page, size) {
      return new Promise(resolve => {
        setTimeout(() => {
          const data = Array.from({ length: size }, (_, i) => ({
            id: (page - 1) * size + i,
            content: `项目 ${(page - 1) * size + i + 1}`
          }))
          resolve(data)
        }, 500)
      })
    },
    handleScroll(e) {
      const { scrollTop, clientHeight, scrollHeight } = e.target
      if (scrollHeight - (scrollTop + clientHeight) < 50 && !this.loading) {
        this.currentPage++
        this.loadItems()
      }
    }
  }
}
</script>

<style>
.infinite-scroll {
  height: 500px;
  overflow-y: auto;
}
.loading {
  text-align: center;
  padding: 10px;
}
</style>

服务端分页

对于大数据量,建议实现服务端分页,只请求当前页的数据。

vue实现翻页

<template>
  <div>
    <table>
      <tr v-for="item in items" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      currentPage: 1,
      totalPages: 1,
      pageSize: 10
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      try {
        const response = await this.$http.get('/api/items', {
          params: {
            page: this.currentPage,
            size: this.pageSize
          }
        })
        this.items = response.data.items
        this.totalPages = response.data.totalPages
      } catch (error) {
        console.error(error)
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.fetchData()
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.fetchData()
      }
    }
  }
}
</script>

以上方法可以根据具体需求选择适合的实现方式。基础分页组件适合简单场景,第三方库提供更多定制选项,无限滚动适合移动端体验,服务端分页处理大数据量更高效。

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

相关文章

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return {…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…