当前位置:首页 > VUE

vue实现查询分页

2026-02-22 15:26:42VUE

Vue 实现查询分页的方法

后端 API 设计

后端需要提供支持分页的 API 接口,通常包含以下参数:

  • page: 当前页码
  • pageSize: 每页显示数量
  • query: 查询条件(可选)

响应数据格式示例:

{
  "data": [],
  "total": 100,
  "page": 1,
  "pageSize": 10
}

前端组件实现

安装 axios 用于请求数据:

npm install axios

创建分页组件 Pagination.vue:

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    totalItems: Number,
    pageSize: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      this.$emit('page-change', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-change', this.currentPage + 1)
    }
  }
}
</script>

主页面实现

在页面组件中使用分页:

<template>
  <div>
    <input v-model="query" placeholder="输入查询条件" @keyup.enter="search" />
    <button @click="search">查询</button>

    <table>
      <tr v-for="item in data" :key="item.id">
        <td>{{ item.name }}</td>
        <!-- 其他字段 -->
      </tr>
    </table>

    <Pagination 
      :current-page="pagination.page"
      :total-items="pagination.total"
      :page-size="pagination.pageSize"
      @page-change="handlePageChange"
    />
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      query: '',
      data: [],
      pagination: {
        page: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const params = {
        page: this.pagination.page,
        pageSize: this.pagination.pageSize,
        query: this.query
      }

      axios.get('/api/data', { params })
        .then(response => {
          this.data = response.data.data
          this.pagination.total = response.data.total
        })
    },
    search() {
      this.pagination.page = 1
      this.fetchData()
    },
    handlePageChange(page) {
      this.pagination.page = page
      this.fetchData()
    }
  }
}
</script>

优化建议

  1. 添加加载状态提示,在请求数据时显示加载动画
  2. 实现页码直接跳转功能
  3. 添加防抖处理搜索输入,避免频繁请求
  4. 使用 Vuex 管理分页状态(大型项目适用)
  5. 考虑添加 URL 参数同步,使页面状态可分享

样式处理

为分页组件添加基础样式:

vue实现查询分页

.pagination {
  margin-top: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.pagination button {
  padding: 5px 10px;
  cursor: pointer;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

这种方法实现了基本的查询分页功能,可以根据实际需求进行扩展和优化。

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

相关文章

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现烟花

vue实现烟花

Vue 实现烟花效果 在 Vue 中实现烟花效果可以通过结合 CSS 动画和 JavaScript 动态生成烟花元素。以下是具体实现方法: 动态生成烟花元素 通过 Vue 的 v-for 动…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…

vue 实现页面返回

vue 实现页面返回

实现页面返回的方法 在Vue中实现页面返回功能可以通过多种方式实现,以下是几种常见的方案: 使用Vue Router的go方法 通过Vue Router的go方法可以控制浏览器的历史记录导航。rou…

vue 实现设置密码

vue 实现设置密码

Vue 实现设置密码功能 在 Vue 中实现设置密码功能通常涉及表单输入、验证和提交逻辑。以下是实现的基本步骤和代码示例。 创建密码表单组件 使用 Vue 的单文件组件(SFC)创建一个密码设置表单…