javascript - 如果在数组中找到单词,则 Vue.js 按下拉列表过滤(仅不完全匹配)
问题描述
https://jsfiddle.net/75f3c2po/
即使有逗号分隔其他单词,如何通过下拉匹配使上述 Vue.js 代码过滤器与整个数组匹配?现在它只匹配,type: BMW
但我希望它也显示可能有类似的卡片type: BMW, Ford
。
另外,我是 Vue 的超级新手,希望有人也可以告诉我如何在select
选择选项后立即更新它,而无需点击“搜索”按钮?
非常感谢
解决方案
为了能够按类型查找,您可以用逗号分隔类型字符串,并检查数组是否包含该值:
this.searchResult = this.items.filter(function(item) {
let filtered = true
if (filterType) {
filtered = item.type.split(',').includes(filterType)
}
if (filtered) {
if (filterCountry && filterCountry.length > 0) {
filtered = item.country == filterCountry
}
}
if (filtered) {
if (filterYear && filterYear.length > 0) {
filtered = item.year == filterYear
}
}
return filtered
})
为了能够立即搜索,您可以@change
在您的选择中使用处理程序:
<select v-model="selectedType" @change="search">
推荐阅读
- javascript - 在这种情况下,我们必须使用这些 Object 字面量、构造函数和 Object.create() 之一,它们之间有何不同?
- php - 使用 php 将 jason 文件发布到 api?
- angular - 未从前端调用补丁 API 调用
- python - 使用 pandas 数据框列中的值更改 DateTime 对象的日期
- rust - 如何在使用 actix-web 代理流式传输大文件时防止超时问题?
- java - .jar UI 在 Linux redhat/centos 7.X 中不起作用
- html - 使用 :not CSS 选择器
- macos - 安装 gnuplot 时 Homebrew/brew 没有选择选项
- android - 没有“clean”或“--rerun-tasks”选项,Gradle 不会运行测试
- javascript - 为什么 Material-UI 无法识别 theme.spacing 功能?