当前位置:首页 > VUE

vue实现搜索过滤

2026-01-08 06:28:18VUE

Vue 实现搜索过滤

使用计算属性实现搜索过滤

在 Vue 中,计算属性(computed)是实现搜索过滤的常见方法。通过计算属性动态过滤数据,无需修改原始数据。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in filteredItems" :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: {
    filteredItems() {
      return this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用方法实现搜索过滤

如果需要在特定事件触发时过滤数据,可以使用方法(methods)实现搜索功能。

<template>
  <div>
    <input v-model="searchQuery" @input="filterItems">
    <ul>
      <li v-for="item in filteredItems" :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: '橙子' }
      ],
      filteredItems: []
    }
  },
  created() {
    this.filteredItems = this.items
  },
  methods: {
    filterItems() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

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

对于更复杂的搜索需求,可以借助第三方库如 lodashdebounce 函数优化性能。

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

<script>
import { debounce } from 'lodash'

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

多条件搜索过滤

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

<template>
  <div>
    <input v-model="searchQuery" placeholder="名称搜索">
    <input v-model="categoryQuery" placeholder="分类搜索">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.category }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      categoryQuery: '',
      items: [
        { id: 1, name: '苹果', category: '水果' },
        { id: 2, name: '香蕉', category: '水果' },
        { id: 3, name: '胡萝卜', category: '蔬菜' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        const nameMatch = item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
        const categoryMatch = item.category.toLowerCase().includes(this.categoryQuery.toLowerCase())
        return nameMatch && categoryMatch
      })
    }
  }
}
</script>

服务器端搜索过滤

对于大量数据,建议将搜索逻辑放在服务器端,通过 API 请求获取过滤后的数据。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      filteredItems: []
    }
  },
  methods: {
    async fetchFilteredItems() {
      try {
        const response = await axios.get('/api/items', {
          params: { search: this.searchQuery }
        })
        this.filteredItems = response.data
      } catch (error) {
        console.error('搜索失败:', error)
      }
    }
  }
}
</script>

vue实现搜索过滤

标签: vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现选中单元格

vue实现选中单元格

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

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…