当前位置:首页 > VUE

vue实现搜索分页

2026-02-17 21:38:30VUE

Vue 实现搜索分页

数据绑定与搜索逻辑

在 Vue 中实现搜索分页功能,需要将搜索关键词与分页参数绑定到数据模型中。通常使用 v-model 绑定搜索输入框,分页参数(如 currentPagepageSize)可通过计算属性或直接绑定。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索关键词" @input="handleSearch" />
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>{{ currentPage }} / {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

计算分页数据

通过计算属性过滤搜索数据并分页。使用 filter 方法根据搜索关键词筛选数据,再通过 slice 方法实现分页。

<script>
export default {
  data() {
    return {
      searchQuery: '',
      currentPage: 1,
      pageSize: 10,
      allData: [] // 原始数据
    };
  },
  computed: {
    filteredData() {
      return this.allData.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    },
    totalPages() {
      return Math.ceil(this.filteredData.length / this.pageSize);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.filteredData.slice(start, end);
    }
  },
  methods: {
    handleSearch() {
      this.currentPage = 1; // 搜索时重置到第一页
    },
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    }
  }
};
</script>

后端分页与 API 集成

如果数据量较大,建议将分页和搜索逻辑交给后端处理。通过 API 传递搜索关键词和分页参数,后端返回分页后的数据。

methods: {
  async fetchData() {
    const params = {
      query: this.searchQuery,
      page: this.currentPage,
      size: this.pageSize
    };
    const response = await axios.get('/api/data', { params });
    this.paginatedData = response.data.items;
    this.totalPages = response.data.totalPages;
  },
  handleSearch() {
    this.currentPage = 1;
    this.fetchData();
  }
}

使用分页组件库

对于更复杂的分页需求,可以集成第三方分页组件(如 element-uiant-design-vue),直接绑定分页事件和参数。

vue实现搜索分页

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

性能优化

对于前端分页,当数据量较大时,可使用虚拟滚动(如 vue-virtual-scroller)减少 DOM 渲染压力。对于后端分页,确保 API 响应速度,合理设置缓存策略。

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

相关文章

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现的页面

vue实现的页面

Vue 实现的页面示例 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是一个简单的 Vue 页面实现示例,包含基本的结构和功能。 安装 Vue 通过 CDN 引入 V…

vue实现数据增加

vue实现数据增加

vue实现数据增加的几种方法 在Vue中实现数据增加通常涉及以下几个核心概念和方法: 响应式数据声明 在Vue组件中,使用data选项或ref(Vue 3)声明响应式数据: // Vue 2 da…

vue实现路由守卫

vue实现路由守卫

路由守卫的基本概念 路由守卫是 Vue Router 提供的一种机制,用于在路由跳转前后执行特定的逻辑。通过路由守卫,可以实现权限控制、页面访问限制、数据预加载等功能。 全局前置守卫 使用 rout…