当前位置:首页 > VUE

vue实现文档翻页

2026-01-19 00:42:41VUE

Vue实现文档翻页的方法

使用v-for和计算属性分页

通过计算属性对数据进行分页处理,结合v-for渲染当前页内容。

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      allData: [] // 这里存放所有文档数据
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用第三方库vue-awesome-paginate

安装vue-awesome-paginate可以快速实现美观的分页组件。

vue实现文档翻页

npm install vue-awesome-paginate
<template>
  <div>
    <div v-for="item in paginatedData" :key="item.id">{{ item.content }}</div>
    <vue-awesome-paginate
      :total-items="allData.length"
      :items-per-page="pageSize"
      v-model="currentPage"
    />
  </div>
</template>

<script>
import { VueAwesomePaginate } from 'vue-awesome-paginate'

export default {
  components: {
    VueAwesomePaginate
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      allData: []
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.allData.slice(start, end)
    }
  }
}
</script>

结合后端API实现分页

对于大量数据,建议采用后端分页方式,每次只请求当前页数据。

vue实现文档翻页

<template>
  <div>
    <div v-for="item in pageData" :key="item.id">{{ item.content }}</div>
    <button @click="loadPage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
    <span>{{ currentPage }}</span>
    <button @click="loadPage(currentPage + 1)" :disabled="isLastPage">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageData: [],
      isLastPage: false
    }
  },
  methods: {
    async loadPage(page) {
      const response = await fetch(`/api/documents?page=${page}`)
      const data = await response.json()
      this.pageData = data.items
      this.currentPage = page
      this.isLastPage = data.isLastPage
    }
  },
  created() {
    this.loadPage(1)
  }
}
</script>

实现无限滚动分页

对于移动端或需要流畅浏览体验的场景,可以使用无限滚动方式。

<template>
  <div class="document-container" @scroll="handleScroll">
    <div v-for="item in loadedData" :key="item.id">{{ item.content }}</div>
    <div v-if="isLoading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loadedData: [],
      currentPage: 1,
      isLoading: false,
      hasMore: true
    }
  },
  methods: {
    async loadMore() {
      if (this.isLoading || !this.hasMore) return

      this.isLoading = true
      const response = await fetch(`/api/documents?page=${this.currentPage}`)
      const data = await response.json()

      this.loadedData = [...this.loadedData, ...data.items]
      this.currentPage++
      this.hasMore = data.hasMore
      this.isLoading = false
    },
    handleScroll(e) {
      const bottomOffset = e.target.scrollHeight - e.target.scrollTop - e.target.clientHeight
      if (bottomOffset < 50) {
        this.loadMore()
      }
    }
  },
  created() {
    this.loadMore()
  }
}
</script>

<style>
.document-container {
  height: 500px;
  overflow-y: auto;
}
</style>

使用Element UI的分页组件

如果项目中使用Element UI,可以直接使用其分页组件。

<template>
  <div>
    <el-table :data="pageData" style="width: 100%">
      <el-table-column prop="content" label="内容"></el-table-column>
    </el-table>
    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="totalItems"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalItems: 0,
      pageData: []
    }
  },
  methods: {
    async handlePageChange(page) {
      this.currentPage = page
      await this.fetchData()
    },
    async fetchData() {
      const response = await fetch(`/api/documents?page=${this.currentPage}&size=${this.pageSize}`)
      const data = await response.json()
      this.pageData = data.items
      this.totalItems = data.total
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

标签: 翻页文档
分享给朋友:

相关文章

jquery文档

jquery文档

以下是关于 jQuery 文档的核心资源和使用方法整理: jQuery 官方文档 jQuery 官方文档是学习和使用 jQuery 最权威的资源,包含详细的 API 说明、示例和更新日志。…

vue翻页实现

vue翻页实现

Vue 翻页实现方法 在 Vue 中实现翻页功能通常需要结合分页组件和数据处理逻辑。以下是几种常见的实现方式: 使用 Element UI 分页组件 Element UI 提供了现成的分页组件 &…

vue 实现翻页

vue 实现翻页

Vue 实现翻页功能 翻页功能通常用于数据分页展示,可以通过 Vue 结合分页逻辑实现。以下是常见的实现方法: 1. 基础数据分页 定义一个数据数组和分页参数,通过计算属性实现分页逻辑。 dat…

jquery文档

jquery文档

以下是关于 jQuery 文档的核心内容和资源整理,便于快速查阅和使用: jQuery 官方文档 官网地址:jQuery Official Documentation 内容分类:API 参考…

vue实现word文档实现预览

vue实现word文档实现预览

实现 Vue 中 Word 文档预览的方法 使用 mammoth.js 将 Word 转换为 HTML 安装依赖: npm install mammoth 在 Vue 组件中引入并转换 .do…

vue实现文档结构

vue实现文档结构

Vue 实现文档结构的方法 Vue 可以通过组件化和路由管理来实现清晰的文档结构。以下是一些关键方法: 组件化拆分 将文档的不同部分拆分为独立的 Vue 组件。例如: 头部导航栏 (Hea…