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,该属性根据输入框的关键字和选择的分类筛选出匹配的项。

相关推荐
abigale0317 分钟前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
专注API从业者37 分钟前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile41 分钟前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_4166399743 分钟前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
我是如子啊1 小时前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火1 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
孤水寒月5 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀5 小时前
html初学者第一天
前端·html
脑袋大大的6 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络7 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码