当前位置:首页 > 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 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const route…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…