当前位置:首页 > VUE

vue实现条件查询

2026-02-17 05:23:34VUE

Vue 实现条件查询的方法

在 Vue 中实现条件查询通常需要结合表单输入、数据绑定和过滤逻辑。以下是几种常见的实现方式:

使用计算属性过滤数据

<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: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 监听查询条件变化

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      filteredItems: []
    }
  },
  watch: {
    searchQuery(newVal) {
      this.filterItems(newVal)
    }
  },
  methods: {
    filterItems(query) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(query.toLowerCase())
      )
    }
  }
}
</script>

结合多个查询条件

<template>
  <input v-model="filters.name" placeholder="名称">
  <input v-model="filters.price" type="number" placeholder="价格">
  <ul>
    <li v-for="product in filteredProducts" :key="product.id">
      {{ product.name }} - {{ product.price }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        name: '',
        price: null
      },
      products: [
        { id: 1, name: '手机', price: 1999 },
        { id: 2, name: '电脑', price: 5999 }
      ]
    }
  },
  computed: {
    filteredProducts() {
      return this.products.filter(product => {
        const nameMatch = product.name.toLowerCase().includes(
          this.filters.name.toLowerCase()
        )
        const priceMatch = this.filters.price 
          ? product.price <= this.filters.price 
          : true
        return nameMatch && priceMatch
      })
    }
  }
}
</script>

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

对于更复杂的搜索需求,可以考虑使用 Fuse.js 等模糊搜索库:

import Fuse from 'fuse.js'

export default {
  data() {
    return {
      search: '',
      list: [...],
      fuseOptions: {
        keys: ['name', 'description'],
        threshold: 0.4
      }
    }
  },
  computed: {
    searchResults() {
      if (!this.search) return this.list
      const fuse = new Fuse(this.list, this.fuseOptions)
      return fuse.search(this.search).map(result => result.item)
    }
  }
}

服务器端条件查询

当数据量较大时,应该考虑将查询条件发送到后端:

vue实现条件查询

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

以上方法可以根据具体需求选择使用,计算属性方式适合前端过滤少量数据,而服务器端查询适合大数据量场景。

标签: 条件vue
分享给朋友:

相关文章

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…