当前位置:首页 > VUE

vue实现搜索过滤

2026-02-10 21:29:11VUE

Vue 实现搜索过滤的方法

使用计算属性过滤列表

通过计算属性实现搜索过滤是最常见的方式。计算属性会根据依赖的数据自动更新,适合处理搜索场景。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  computed: {
    filteredList() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 监听搜索词变化

当搜索逻辑较复杂或需要异步操作时,可以使用 watch 监听搜索词变化。

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      filteredItems: []
    }
  },
  watch: {
    searchQuery(newVal) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(newVal.toLowerCase())
      )
    }
  },
  created() {
    // 假设这里是异步获取数据
    this.items = [
      { id: 1, name: '苹果' },
      { id: 2, name: '香蕉' },
      { id: 3, name: '橙子' }
    ]
    this.filteredItems = [...this.items]
  }
}
</script>

使用第三方库实现高级搜索

对于更复杂的搜索需求,可以使用 lodash 的 _.debounce 实现防抖搜索,减少不必要的计算。

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      filteredItems: []
    }
  },
  created() {
    this.debouncedFilter = _.debounce(this.filterItems, 300)
    this.items = [
      { id: 1, name: '苹果' },
      { id: 2, name: '香蕉' },
      { id: 3, name: '橙子' }
    ]
    this.filteredItems = [...this.items]
  },
  methods: {
    filterItems() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  },
  watch: {
    searchQuery() {
      this.debouncedFilter()
    }
  }
}
</script>

多条件搜索过滤

当需要根据多个条件进行搜索时,可以扩展过滤逻辑。

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

服务器端搜索

对于大量数据,建议将搜索请求发送到服务器处理。

vue实现搜索过滤

methods: {
  async searchItems() {
    try {
      const response = await axios.get('/api/items', {
        params: {
          q: this.searchQuery
        }
      })
      this.filteredItems = response.data
    } catch (error) {
      console.error('搜索失败:', error)
    }
  }
},
watch: {
  searchQuery: _.debounce(function(newVal) {
    this.searchItems()
  }, 500)
}

以上方法涵盖了从简单到复杂的各种搜索过滤场景,可以根据实际需求选择适合的实现方式。计算属性适合客户端简单过滤,watch 适合需要更多控制的情况,而服务器端搜索则适合处理大数据量。

标签: vue
分享给朋友:

相关文章

vue 实现手册

vue 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组件…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 meth…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现影院

vue实现影院

Vue实现影院功能的基本思路 使用Vue框架开发影院功能需要结合前端组件化开发、状态管理以及后端API交互。以下是关键实现步骤: 影院页面布局设计 影院页面通常分为顶部导航栏、影院列表区域和底部导航…