I have two options that I am trying to filter between now, but will be adding in a third and maybe a fourth later on (for example: right now I am filtering between prices and reviews but will want to add more prices to the list and also another filter category like “rating” which would consist of 3, 4, or 5 stars).
Using the logic that I have listed below works fine, but I feel it will get really long and complicated (and unnecessary) as I go. I know there is a way to refactor the code I’m just wondering what the best way to go about it would be?
HTML:
<select class="deals">
<option value="deals-all">All deals</option>
<option value="50">$50</option>
<option value="25">$25</option>
</select>
<select class="reviews">
<option value="reviews-all">All reviews</option>
<option value="reviews-positive">Positive reviews</option>
<option value="reviews-negative">Negative reviews</option>
</select>
jQuery
$('.reviews, .deals').change(function() {
var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();
if((reviewsVal == 'reviews-all') && (dealsVal == 'deals-all')) {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-positive')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').show();
}
else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-negative')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '50') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').show();
$('.review-positive.deals-50').show();
$('.review-negative.deals-25').hide();
$('.review-positive.deals-25').hide();
}
else if((dealsVal == '25') && (reviewsVal == 'reviews-all')) {
$('.review-negative.deals-50').hide();
$('.review-positive.deals-50').hide();
$('.review-negative.deals-25').show();
$('.review-positive.deals-25').show();
}
else {
$('.review-positive').show();
$('.review-negative').show();
$('.deals-25').show();
$('.deals-50').show();
}
});
$('.reviews-positive').click(function() {
$('.review-negative').hide();
$('.review-positive').show();
});
$('.reviews-negative').click(function() {
$('.review-positive').hide();
$('.review-negative').show();
});
});
Hopefully you can see what I am going for, thanks for any input.
*EDIT: jsfiddle: http://jsfiddle.net/TXywp/1/
To get a dynamic result a standardized naming is the best way to go.
You start by standardizing the criteria selectors, giving them a general class and differentiating with id properties. Following a naming convetion for values is also recommended. This would produce something in the lines of
Then you give all the items you want to filter a generic class besides the filtering classes. I will use .item as an example. You will also want to be able to construct the class from selected value and criteria name so using the .{id}-{value} system will work perfectly. This will leave us with something in the lines of
This setup will allow us to build dynamic and extensible code:
Now adding a new criteria selector won’t require changes in this piece of code as long as naming conventions are followed.