Skip to content Skip to sidebar Skip to footer

Multiple Condition Filter

I am using multiple checkbox to filter it inside booksFilter function. data() { return { books:[ { name:'Book1', price:2000 },

Solution 1:

What about:

booksFilter() {
  returnthis.books.filter(x => {
    if (this.filters.options.length> 0) {
      boolean condition = true; 
      if (this.filters.options.indexOf(0)!=-1) {
         conditon = conditon && x.price>1000
      }

      if (this.filters.options.indexOf(1)!=-1) {
         conditon = conditon && x.name == "Book1";
      }

      return condition;
    else {
      returntrue;
    }  
  })
}

Hope it will resolve your case.

Solution 2:

It seems as your filter is incorrect.

For simplicity purposes i divided into 2 sequential filters, one for each checkbox:

computed:{
    booksFilter(){
      returnthis.books.filter(x => {
        if(this.filters.options.length && this.filters.options.indexOf(0)!=-1){
          return x.price>1000;
        }
        else{
          returntrue;
        }  
      }).filter(x => {
        if(this.filters.options.length && this.filters.options.indexOf(1)!=-1){
          return x.name == "Book1";
        }
        else{
          returntrue;
        }  
      })
    }
  }  

Solution 3:

Update your logic like that to achieve the result:

return (this.filters.options.indexOf(0) === -1 || x.price > 1000)
  && (this.filters.options.indexOf(1) === -1 || x.name === "Book1");

this.filters.options.indexOf(0) === -1 || x.price > 1000 - filter 0 is not selected, return true OR filter by priceANDthis.filters.options.indexOf(1) === -1 || x.name === "Book1" - filter 1 is not selected, return true OR filter by name

Post a Comment for "Multiple Condition Filter"