当前位置:首页 > 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>

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

相关文章

vue实现创建文档

vue实现创建文档

Vue 实现创建文档的方法 使用 Vue CLI 创建项目 通过 Vue CLI 可以快速初始化一个 Vue 项目。确保已安装 Node.js 和 npm/yarn,运行以下命令安装 Vue CLI:…

vue翻页实现

vue翻页实现

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

elementui  文档

elementui 文档

以下是 Element UI 官方文档及相关资源的整理: 官方文档地址 Element UI 的官方文档可通过以下链接访问: 中文文档:https://element.eleme.cn…

vue实现word文档实现预览

vue实现word文档实现预览

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

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div>…

vue实现文档结构

vue实现文档结构

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