当前位置:首页 > VUE

vue实现分页查询

2026-01-19 12:40:02VUE

Vue 实现分页查询

基本思路

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

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

实现步骤

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

npm install element-ui

基础实现代码

<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 要求

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

vue实现分页查询

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

优化建议

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

注意事项

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

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

相关文章

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…