I am developing a javascript filtering of some results and have some difficulties..
Here is the problem…
Suppose that we have some criteria
- Manufacter (Trusardi, Calvin Kein, Armani…)
- Color (red, blue, black…)
- OtherFeatures (goretex, blah, blah…)
Each feature is displayed as a checkbox…
The problem is that i want to disable the checkboxes if its selection would lead to no results..For example Armani’s products may have color blue only so checking armani should disable the black and red,but other manufactors shouldnt be disabled… as checking them should provide a result…
Here is the code so far
results = $("#products li");
results.hide();
var filtersGroup = $("#filters li.filtersGroup");
$("#filters li.filtersGroup a").removeClass("disabled");
filtersGroup.each(function(index) {
var classes = "";
$(this).find("a.checked").each(function(index) {
classes = classes + "." + $(this).attr("id") + ",";
});
if (classes == "") return true;
results = results.filter(classes.substr(0, classes.length - 1));
//periorismos
filtersGroup.not($(this)).each(function(index) {
$(this).find("a").each(function(index) {
if (results.filter("." + $(this).attr("id")).length <= 0) {
$(this).removeClass("checked").addClass("disabled");
}
});
});
});
Although it filters them successfully ,the disabling isnt always correct. for example to reproduce the problem ,if you choose all the manufactors and then choose a color manufactors would be disabled but colors not at the first time..
One solution i figured is to create multiple results which would simulate all the next possible check.(if 16 features and 4 checked means 12 possible other checked ..
But i think that this approach sucks… Any other idea?
You could add a class for each manufacturer, colour, etc. then simply disable by class. I assume here there’s a results hash keyed off manufacturer:
..then:
etc.