mardi 26 janvier 2016

Jquery filtering with multiple Checkboxes

For a current project I am creating a simple product catalogue which should be able to be filtered by Jquery with several sets of checkboxes.

In one single set when two or more checkboxes are checked the logic should be OR while when using two or more sets of checkboxes it should be AND.

A good (and working) example I am currently using can be found here:

http://ift.tt/1KBChv8

JSFIDDLE

if (cselector === '' && nselector === '') {         
$('.flowers > div').filter(selector).show();
} else if (cselector === '') {
$('.flowers > div').filter(selector).filter(nselector).show();
} else if (nselector === '') {
$('.flowers > div').filter(selector).filter(cselector).show();
} else {
$('.flowers > div').filter(selector).filter(cselector).filter(nselector).show();
}

My Question regards this part of the code where the filters are being used in case they were selected. Now this works pretty good for the 3 filters that are being used in the example. What I am wondering is there some better way to be doing this? In my case I want to add some more filters and then this part of the code will get messy pretty soon when I have to add a lot more if statements.

So basically my question is: Using this setup, how would you add more filters so you have 6 or 7 in the end instead of the current 3?

Thanks a lot, Peter

P.S. I also appreciate tips for completly different approaches but as I already put quite some time into this I would really like to get it work this way.




Aucun commentaire:

Enregistrer un commentaire