当前位置:首页 > VUE

vue列表查询实现

2026-02-10 18:39:17VUE

实现 Vue 列表查询功能

1. 数据绑定与列表渲染

使用 v-for 指令渲染列表数据,绑定到组件的 dataprops

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

<script>
export default {
  data() {
    return {
      rawList: [{id: 1, name: 'Apple'}, {id: 2, name: 'Banana'}],
      searchQuery: ''
    }
  }
}
</script>

2. 计算属性实现过滤

通过计算属性动态过滤列表,避免每次输入都触发方法调用:

computed: {
  filteredList() {
    return this.rawList.filter(item => 
      item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    )
  }
}

3. 添加搜索输入框

在模板中添加搜索输入框,使用 v-model 双向绑定搜索关键词:

<input 
  type="text" 
  v-model="searchQuery" 
  placeholder="Search..."
  class="search-input"
>

4. 优化搜索性能

对于大数据量列表,可添加防抖处理:

import _ from 'lodash';

export default {
  methods: {
    search: _.debounce(function() {
      // 执行搜索逻辑
    }, 300)
  }
}

5. 服务端搜索实现

当数据量较大时,可改为服务端搜索:

methods: {
  async fetchSearchResults() {
    const res = await axios.get('/api/items', {
      params: { q: this.searchQuery }
    });
    this.filteredList = res.data;
  }
},
watch: {
  searchQuery(newVal) {
    if(newVal.length > 2) this.fetchSearchResults();
  }
}

6. 高级过滤功能

实现多条件复合搜索:

computed: {
  filteredList() {
    return this.rawList.filter(item => {
      const matchesName = item.name.toLowerCase().includes(this.searchQuery.toLowerCase());
      const matchesCategory = this.selectedCategory ? 
        item.category === this.selectedCategory : true;
      return matchesName && matchesCategory;
    });
  }
}

7. 空状态提示

添加搜索结果为空时的提示:

vue列表查询实现

<template>
  <div v-if="filteredList.length === 0" class="empty-state">
    No items found
  </div>
</template>

标签: 列表vue
分享给朋友:

相关文章

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$router…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

轮播vue实现

轮播vue实现

使用 Vue 实现轮播功能 基础轮播实现 安装依赖(如使用 Swiper): npm install swiper vue-awesome-swiper 组件代码示例: <template&…

vue实现插件

vue实现插件

Vue 插件实现方法 Vue 插件通常用于为 Vue 应用添加全局功能或共享功能。以下是实现 Vue 插件的核心步骤: 插件基本结构 Vue 插件需要暴露一个 install 方法,该方法接收 Vu…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…