当前位置:首页 > VUE

vue搜索分页实现

2026-02-19 00:51:16VUE

Vue 搜索分页实现

基本思路

在Vue中实现搜索分页功能,通常需要结合后端API接口,前端处理搜索关键词、分页参数以及数据渲染。以下是实现的核心步骤和代码示例。

数据准备

定义组件的数据结构,包括搜索关键词、分页参数、列表数据等。

data() {
  return {
    searchQuery: '', // 搜索关键词
    currentPage: 1,   // 当前页码
    pageSize: 10,     // 每页显示条数
    total: 0,         // 总数据量
    list: [],         // 列表数据
    loading: false    // 加载状态
  }
}

搜索方法

封装搜索请求方法,调用后端API并处理返回结果。

vue搜索分页实现

methods: {
  async fetchData() {
    this.loading = true;
    try {
      const params = {
        keyword: this.searchQuery,
        page: this.currentPage,
        size: this.pageSize
      };
      const res = await api.getList(params); // 假设api是封装好的请求方法
      this.list = res.data.list;
      this.total = res.data.total;
    } catch (error) {
      console.error('搜索失败:', error);
    } finally {
      this.loading = false;
    }
  }
}

分页组件

使用Element UI或其他UI库的分页组件,绑定分页参数和事件。

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

事件处理

处理分页切换和搜索事件,触发数据重新加载。

vue搜索分页实现

methods: {
  handlePageChange(page) {
    this.currentPage = page;
    this.fetchData();
  },
  handleSearch() {
    this.currentPage = 1; // 搜索时重置页码
    this.fetchData();
  }
}

防抖优化

为避免频繁触发搜索请求,可以为搜索输入框添加防抖功能。

import { debounce } from 'lodash';

methods: {
  handleSearch: debounce(function() {
    this.currentPage = 1;
    this.fetchData();
  }, 500)
}

完整示例

以下是一个完整的Vue组件示例:

<template>
  <div>
    <el-input v-model="searchQuery" placeholder="请输入关键词" @input="handleSearch"></el-input>
    <el-table :data="list" v-loading="loading">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="total, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
import { debounce } from 'lodash';

export default {
  data() {
    return {
      searchQuery: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      list: [],
      loading: false
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    handleSearch: debounce(function() {
      this.currentPage = 1;
      this.fetchData();
    }, 500),
    async fetchData() {
      this.loading = true;
      try {
        const params = {
          keyword: this.searchQuery,
          page: this.currentPage,
          size: this.pageSize
        };
        const res = await api.getList(params);
        this.list = res.data.list;
        this.total = res.data.total;
      } catch (error) {
        console.error('搜索失败:', error);
      } finally {
        this.loading = false;
      }
    }
  },
  mounted() {
    this.fetchData();
  }
}
</script>

注意事项

  • 确保后端API支持关键词搜索和分页参数。
  • 分页组件的total属性需要与后端返回的总数据量一致。
  • 加载状态loading可以提升用户体验,避免重复请求。

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

相关文章

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕包…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…