当前位置:首页 > VUE

vue实现分页查询

2026-01-19 12:40:02VUE

Vue 实现分页查询

基本思路

在 Vue 中实现分页查询通常需要结合后端 API 和前端组件。核心逻辑包括:

  • 通过 API 获取分页数据
  • 处理分页参数(当前页、每页条数)
  • 渲染分页控件
  • 监听页码变化事件

实现步骤

安装依赖(可选) 如果需要使用现成的分页组件,可以安装 element-uivant 等 UI 库:

vue实现分页查询

npm install element-ui

基础实现代码

vue实现分页查询

<template>
  <div>
    <!-- 数据表格 -->
    <table>
      <tr v-for="item in list" :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 {
      list: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.total / this.pageSize)
    }
  },
  methods: {
    async fetchData() {
      const res = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      })
      this.list = res.data.list
      this.total = res.data.total
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.fetchData()
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
        this.fetchData()
      }
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

使用 Element UI 分页组件

如果使用 Element UI,可以简化分页控件实现:

<template>
  <div>
    <el-table :data="list">
      <el-table-column prop="name" label="名称"></el-table-column>
    </el-table>

    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}
</script>

后端 API 要求

后端接口通常需要支持以下参数:

  • page: 当前页码
  • size: 每页条数 并返回格式如:
    {
    "list": [...],
    "total": 100
    }

优化建议

  • 添加加载状态指示
  • 实现页码跳转功能
  • 支持每页条数动态调整
  • 添加防抖处理频繁请求

注意事项

  • 确保分页参数正确传递给后端
  • 处理无数据情况
  • 考虑大数据量时的性能优化

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

相关文章

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…