当前位置:首页 > VUE

vue实现列表搜索

2026-01-19 21:54:57VUE

实现列表搜索的基本思路

在Vue中实现列表搜索功能,通常需要结合v-model绑定搜索关键词,通过计算属性或方法过滤原始数据。核心是监听输入变化并实时更新展示结果。

基础实现方法

创建Vue实例或组件,定义数据列表和搜索关键词

data() {
  return {
    searchQuery: '',
    items: [
      { id: 1, name: 'Apple' },
      { id: 2, name: 'Banana' },
      { id: 3, name: 'Orange' }
    ]
  }
}

使用计算属性实现过滤逻辑

computed: {
  filteredItems() {
    const query = this.searchQuery.toLowerCase()
    return this.items.filter(item => 
      item.name.toLowerCase().includes(query)
    )
  }
}

模板部分绑定数据和展示结果

vue实现列表搜索

<input v-model="searchQuery" placeholder="Search...">
<ul>
  <li v-for="item in filteredItems" :key="item.id">
    {{ item.name }}
  </li>
</ul>

多字段搜索扩展

当需要搜索多个字段时,调整过滤逻辑

computed: {
  filteredItems() {
    const query = this.searchQuery.toLowerCase()
    return this.items.filter(item => 
      item.name.toLowerCase().includes(query) ||
      item.description.toLowerCase().includes(query)
    )
  }
}

使用自定义过滤方法

对于复杂搜索逻辑,可以使用方法替代计算属性

vue实现列表搜索

methods: {
  filterItems(query) {
    return this.items.filter(item => 
      // 自定义过滤条件
    )
  }
}

添加搜索延迟优化

通过lodashdebounce减少频繁触发

import { debounce } from 'lodash'

methods: {
  search: debounce(function(query) {
    // 执行搜索
  }, 300)
}

服务器端搜索实现

当数据量较大时,建议使用后端搜索API

methods: {
  async searchItems() {
    const response = await axios.get('/api/items', {
      params: { q: this.searchQuery }
    })
    this.filteredItems = response.data
  }
}

样式和交互增强

添加加载状态和空结果提示

<div v-if="loading">Searching...</div>
<div v-else-if="filteredItems.length === 0">
  No results found
</div>

完整组件示例

<template>
  <div>
    <input 
      v-model="searchQuery" 
      @input="onSearch"
      placeholder="Search items..."
    >
    <ul v-if="!loading">
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.price }}
      </li>
    </ul>
    <p v-if="loading">Loading...</p>
    <p v-if="!loading && filteredItems.length === 0">
      No items match your search
    </p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      loading: false,
      allItems: [],
      filteredItems: []
    }
  },
  async created() {
    this.loading = true
    this.allItems = await fetchItems()
    this.filteredItems = this.allItems
    this.loading = false
  },
  methods: {
    onSearch() {
      this.loading = true
      this.filteredItems = this.allItems.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        item.description.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
      this.loading = false
    }
  }
}
</script>

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

相关文章

vue双向实现

vue双向实现

Vue 双向数据绑定的实现原理 Vue 的双向数据绑定主要通过 v-model 指令实现,其核心是结合了数据劫持(Object.defineProperty 或 Proxy)和发布-订阅模式。 数…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…