多条件过滤器 [英] Multiple condition filter
本文介绍了多条件过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 booksFilter 函数中使用多个复选框来过滤它.
I am using multiple checkbox to filter it inside booksFilter function.
data() {
return {
books:[
{
name:"Book1",
price:2000
},
{
name:"Book2",
price:2000
},
{
name:"Book3",
price:2000
},
{
name:"Book1",
price:1000
}
],
filters:{
options:[]
}
}
},
computed:{
booksFilter(){
return this.books.filter(x => {
if(this.filters.options.length>0){
return this.filters.options.indexOf(0)!=-1 && x.price>1000
|| this.filters.options.indexOf(1)!=-1 && x.name == "Book1";
}
else{
return true;
}
})
}
https://codepen.io/Aksultan/pen/NWKeZGg
当两个复选框都被使用时,它应该只返回一本书,但现在它返回整个数据.
It should return only one book, when both checkbox is used, but now it returns entire data.
谢谢
推荐答案
您的过滤器似乎不正确.
It seems as your filter is incorrect.
为了简单起见,我分为 2 个连续过滤器,每个复选框一个:
For simplicity purposes i divided into 2 sequential filters, one for each checkbox:
computed:{
booksFilter(){
return this.books.filter(x => {
if(this.filters.options.length && this.filters.options.indexOf(0)!=-1){
return x.price>1000;
}
else{
return true;
}
}).filter(x => {
if(this.filters.options.length && this.filters.options.indexOf(1)!=-1){
return x.name == "Book1";
}
else{
return true;
}
})
}
}
这篇关于多条件过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文