当前位置:首页 > VUE

vue实现ajax数据分页

2026-01-22 15:06:24VUE

Vue 实现 AJAX 数据分页的方法

使用 axios 发送 AJAX 请求

安装 axios 并导入到 Vue 组件中:

npm install axios

在组件中引入 axios:

import axios from 'axios';

发送分页请求示例:

methods: {
  fetchData(page = 1) {
    axios.get('/api/data', {
      params: {
        page: page,
        per_page: 10
      }
    })
    .then(response => {
      this.items = response.data.data;
      this.total = response.data.total;
    })
    .catch(error => {
      console.error(error);
    });
  }
}

分页组件实现

在模板中添加分页控件:

vue实现ajax数据分页

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <div class="pagination">
      <button 
        @click="fetchData(currentPage - 1)" 
        :disabled="currentPage === 1"
      >
        上一页
      </button>

      <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>

      <button 
        @click="fetchData(currentPage + 1)" 
        :disabled="currentPage === totalPages"
      >
        下一页
      </button>
    </div>
  </div>
</template>

计算属性处理分页逻辑

添加计算属性计算总页数:

computed: {
  totalPages() {
    return Math.ceil(this.total / this.perPage);
  }
},
data() {
  return {
    items: [],
    total: 0,
    currentPage: 1,
    perPage: 10
  };
}

服务器端分页处理

Node.js Express 示例:

vue实现ajax数据分页

app.get('/api/data', (req, res) => {
  const page = parseInt(req.query.page) || 1;
  const perPage = parseInt(req.query.per_page) || 10;
  const offset = (page - 1) * perPage;

  // 假设使用 Sequelize
  Model.findAndCountAll({
    limit: perPage,
    offset: offset
  })
  .then(result => {
    res.json({
      data: result.rows,
      total: result.count
    });
  });
});

优化用户体验

添加加载状态提示:

data() {
  return {
    isLoading: false
  };
},
methods: {
  fetchData(page = 1) {
    this.isLoading = true;
    axios.get('/api/data', {
      params: {
        page: page,
        per_page: 10
      }
    })
    .then(response => {
      this.items = response.data.data;
      this.total = response.data.total;
      this.currentPage = page;
    })
    .finally(() => {
      this.isLoading = false;
    });
  }
}

模板中添加加载状态:

<div v-if="isLoading">加载中...</div>

分页参数持久化

使用 Vue Router 保持分页状态:

watch: {
  currentPage(newVal) {
    this.$router.push({
      query: {
        ...this.$route.query,
        page: newVal
      }
    });
  }
},
created() {
  const page = parseInt(this.$route.query.page) || 1;
  this.fetchData(page);
}

标签: 分页数据
分享给朋友:

相关文章

vue实现数据翻译

vue实现数据翻译

Vue 实现数据翻译的方法 在 Vue 项目中实现数据翻译(国际化)通常需要结合国际化库或自定义方案。以下是几种常见的方法: 使用 vue-i18n 库 安装 vue-i18n 库: npm in…

分页用vue实现

分页用vue实现

分页用 Vue 实现 在 Vue 中实现分页功能通常需要结合数据管理和 UI 组件。以下是几种常见的方法: 使用 Vue 组件库的分页组件 许多 Vue UI 组件库(如 Element UI、V…

vue实现分页功能

vue实现分页功能

Vue 分页功能实现 在 Vue 中实现分页功能通常需要结合后端 API 或前端数据分页逻辑。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在 Vue 组件中处理分页逻辑…

vue实现表格读取数据

vue实现表格读取数据

Vue 实现表格数据读取 在 Vue 中实现表格数据读取通常涉及以下关键步骤: 数据绑定与表格渲染 通过 v-for 指令动态渲染表格行,数据通常存储在组件的 data 或 Vuex 状态中:…

vue实现重置表格数据

vue实现重置表格数据

重置表格数据的方法 在Vue中重置表格数据通常涉及清空或重新初始化数据绑定的数组或对象。以下是几种常见实现方式: 直接重置数据源 对于基于数组的表格数据,可以直接将数据数组重置为空或初始值:…

vue怎么实现拖动数据

vue怎么实现拖动数据

实现拖动数据的基本方法 在Vue中实现拖动数据通常使用HTML5的拖放API或第三方库如vuedraggable。以下是两种常见方法的详细说明: 使用HTML5原生拖放API HTML5提供了dra…