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

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

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

优化建议

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

注意事项

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

vue实现分页查询

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

相关文章

前端多线程实现vue

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker的…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设…