vue多条件查询

编程入门 行业动态 更新时间:2024-10-10 15:22:05

vue<a href=https://www.elefans.com/category/jswz/34/1769946.html style=多条件查询"/>

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

更多推荐

vue多条件查询

本文发布于:2023-12-06 22:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1669093.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多条   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!