当前位置:首页 > VUE

vue实现input动态搜索

2026-02-21 09:53:53VUE

Vue 实现 input 动态搜索

使用 v-model 和 @input 事件

通过 v-model 绑定输入框的值,结合 @input 事件触发搜索逻辑。debounce 可以避免频繁触发搜索。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [], // 原始数据
      filteredItems: [] // 过滤后的数据
    }
  },
  methods: {
    handleSearch() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 监听搜索词

通过 watch 监听 searchQuery 的变化,触发搜索逻辑。可以结合 debounce 优化性能。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [], // 原始数据
      filteredItems: [] // 过滤后的数据
    }
  },
  watch: {
    searchQuery(newVal) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }
  }
}
</script>

使用 computed 计算属性

通过 computed 动态计算过滤后的数据,响应式更新搜索结果。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [] // 原始数据
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 Lodash 的 debounce 优化性能

避免频繁触发搜索逻辑,使用 debounce 延迟执行搜索。

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

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [], // 原始数据
      filteredItems: [] // 过滤后的数据
    }
  },
  created() {
    this.debouncedSearch = debounce(this.handleSearch, 300)
  },
  methods: {
    handleSearch() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

结合 API 请求实现远程搜索

通过输入框触发 API 请求,获取远程搜索结果。

<template>
  <input v-model="searchQuery" @input="debouncedSearch" placeholder="搜索...">
  <ul>
    <li v-for="item in searchResults" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      searchResults: [] // 远程搜索结果
    }
  },
  created() {
    this.debouncedSearch = debounce(this.fetchSearchResults, 300)
  },
  methods: {
    async fetchSearchResults() {
      if (this.searchQuery.trim() === '') {
        this.searchResults = []
        return
      }
      try {
        const response = await axios.get('/api/search', {
          params: { q: this.searchQuery }
        })
        this.searchResults = response.data
      } catch (error) {
        console.error('搜索失败:', error)
      }
    }
  }
}
</script>

注意事项

  • 对于本地搜索,computed 是最简洁的方式。
  • 远程搜索建议使用 debouncethrottle 减少请求频率。
  • 搜索逻辑可以结合正则表达式或其他过滤条件增强功能。

vue实现input动态搜索

标签: 动态vue
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-mode…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue实现展开

vue实现展开

展开功能的实现方法 在Vue中实现展开功能可以通过多种方式完成,常见的有使用v-show、v-if指令,结合CSS过渡动画或第三方组件库。 使用v-show控制显示隐藏 通过v-show指令可以简单…