vue多条件查询

复制代码
<template>
  <div>
    <input type="text" v-model="keyword" placeholder="关键字">
    <select v-model="category">
      <option value="">所有分类</option>
      <option v-for="cat in categories" :value="cat">{{ cat }}</option>
    </select>
    <button @click="search">查询</button>
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.category }}
      </li>
    </ul>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    const keyword = ref('');
    const category = ref('');
    const items = ref([
      { id: 1, name: '物品1', category: '分类1' },
      { id: 2, name: '物品2', category: '分类2' },
      { id: 3, name: '物品3', category: '分类1' },
      { id: 4, name: '物品4', category: '分类3' }
    ]);
    const categories = ['分类1', '分类2', '分类3'];

    const filteredItems = computed(() => {
      return items.value.filter(item => {
        const isMatchingKeyword = item.name.toLowerCase().includes(keyword.value.toLowerCase());
        const isMatchingCategory = !category.value || item.category === category.value;
        return isMatchingKeyword && isMatchingCategory;
      });
    });

    const search = () => {
      // 执行搜索逻辑(例如调用接口)
      // 根据输入框和条件筛选出匹配的项
      // 更新 filteredItems
    };

    return {
      keyword,
      category,
      categories,
      filteredItems,
      search
    };
  }
};
</script>

在上述例子中,我们使用了Vue 3的refcomputed函数。首先,我们创建了名为keywordcategoryitems的响应式引用。然后,我们使用computed函数创建了一个计算属性filteredItems,该属性根据输入框的关键字和选择的分类筛选出匹配的项。

相关推荐
摘星编程4 小时前
在OpenHarmony上用React Native:Spinner自定义样式
javascript·react native·react.js
摇滚侠4 小时前
css 设置边框
前端·css
星爷AG I4 小时前
9-24 视觉叙事(AGI基础理论)
前端·人工智能
2501_940007894 小时前
Flutter for OpenHarmony三国杀攻略App实战 - 鸿蒙适配与打包发布
前端·flutter
css趣多多4 小时前
跨域问题及Vue项目中本地/线上解决方法核心总结
前端
光影少年4 小时前
前端 AIGC
前端·aigc
启山智软5 小时前
供应链商城核心功能模块清单
java·前端·开源
徐同保5 小时前
Claude Code提示词案例(开发复杂动态路由详情页面)
前端
Σdoughty5 小时前
python第三次作业
开发语言·前端·python
是萧萧吖5 小时前
每日一练——有效的括号
java·开发语言·javascript