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

相关推荐
Onlyᝰ2 小时前
前端tree树
javascript·vue.js·elementui
高德开放平台3 小时前
实战案例|借助高德开放平台实现智慧位置服务:路线导航的开发与实践
前端·后端
hemoo3 小时前
如何让echart的lengend在指定位置换行
javascript·echarts
老前端的功夫3 小时前
# HTTP缓存:从懵懵懂懂到了如指掌
前端
安卓开发者3 小时前
Docker与Nginx:现代Web部署的完美二重奏
前端·nginx·docker
Dorian_Ov03 小时前
GeoPandas+DataFrame实现shapefile文件导入PostGIS数据库
前端·gis
葡萄城技术团队3 小时前
Vue 生态下前端 Excel 导入导出终极方案:SpreadJS 实战指南
vue.js
哟哟耶耶3 小时前
Starting again company 03
前端·javascript·vue.js
葡萄城技术团队3 小时前
SpreadJS 赋能在线 Excel:协同编辑与精细化权限管控的技术实现
前端
Chloe_lll3 小时前
threejs(七)PBR材质
开发语言·javascript·材质