Hi Mathias,
the current code does one thing - it takes a group of related icons and applies filter an OR condition between them. So, if user selects Aero and Marathon, you get all bikes that are either Aero or Marathon. If you add more choices unrelated to bike category, you are effectively increasing the chance by which a bike can match a given query. In other words, if user selects Electric shifter on top of Aero and Marathon selections, site shows all bikes that are either Aero or Marathon or have electric shifter.
To fix it, let’s create a function that will transform a group of related icons into a filter in such way that it can work with any group. It’s purpose is the same as we had before, but now it does not assume what kind of options there are and where to put this filter. It simply constructs the filter based on selections and returns it:
function createFilterGroup(allOptions) {
const selectedOptions = allOptions.filter(choiceDescriptor => {
const optionSelected = !$w(choiceDescriptor.element).hidden
return optionSelected;
})
const filter = selectedOptions.reduce((filter, choiceDescriptor, index) => {
return index === 0 ?
filter.eq(choiceDescriptor.field, true) :
filter.or(wixData.filter().eq(choiceDescriptor.field, true));
}, wixData.filter());
return filter;
}
Next, let’s define some groups:
const categories = [
{ field: "aero", element: "#aeroiconRed2" },
{ field: "allround", element: "#allroundIconRed2" },
...
]
const brakes = [
{ field: "rimBrake", element: "#brakeIconRed2" },
{ field: "discBrake", element: "#discIconRed2"},
...
]
const shifters = [
...
]
const brands = [
...
]
Finally, we need to convert each group into a filter by using the function we created before and add them to main filter:
let newChoices = wixData.filter();
newChoices = newChoices
.and(createFilterGroup(categories))
.and(createFilterGroup(brakes))
.and(createFilterGroup(shifters))
.and(createFilterGroup(brands))
Full code for convenience:
function createFilterGroup(allOptions) {
const selectedOptions = allOptions.filter(choiceDescriptor => {
const optionSelected = !$w(choiceDescriptor.element).hidden
return optionSelected;
})
const filter = selectedOptions.reduce((filter, choiceDescriptor, index) => {
return index === 0 ?
filter.eq(choiceDescriptor.field, true) :
filter.or(wixData.filter().eq(choiceDescriptor.field, true));
}, wixData.filter());
return filter;
}
function applyFilter() {
const categories = [
{ field: "aero", element: "#aeroiconRed2" },
{ field: "allround", element: "#allroundIconRed2" },
{ field: "marathon", element: "#marathonIconRed2" },
{ field: "tri", element: "#triIconRed2" },
{ field: "gravel", element: "#graveliconRed2" }
]
const brakes = [
{ field: "rimBrake", element: "#brakeIconRed2" },
{ field: "discBrake", element: "#discIconRed2"},
{ field: "rimBrake", element: "#brakeIconRed2" },
{ field: "discBrake", element: "#discIconRed2"},
]
const shifters = [
{ field: "electricShifter", element: "#electricIconRed2"},
{ field: "mechanicShifter", element: "#mechanicIconRed2"},
]
const brands = [
{ field: "argon18", element: "#argon18IconRed2"},
{ field: "bianchi", element: "#bianchiIconRed2"},
{ field: "bmc", element: "#bmcIconRed2"},
{ field: "cannondale", element: "#cannondaleIconRed2"},
{ field: "canyon", element: "#canyonIconRed2"},
{ field: "cervelo", element: "#cerveloIconRed2"},
{ field: "cubeBikes", element: "#cubeIconRed2"},
{ field: "felt", element: "#feltIconRed2"},
{ field: "focus", element: "#focusIconRed2"},
{ field: "fuji", element: "#fujiIconRed2"},
{ field: "giant", element: "#giantIconRed2"},
{ field: "look", element: "#lookIconRed2"},
{ field: "merida", element: "#meridaIconRed2"},
{ field: "orbea", element: "#orbeaIconRed2"},
{ field: "pinarello", element: "#pinarelloIconRed2"},
{ field: "rose", element: "#roseIconRed2"},
{ field: "scott", element: "#scottIconRed2"},
{ field: "specialized", element: "#specializedIconRed2"},
{ field: "storck", element: "#storckIconRed2"},
{ field: "trek", element: "#trekIconRed2"}
]
let newChoices = wixData.filter();
newChoices = newChoices
.and(createFilterGroup(categories))
.and(createFilterGroup(brakes))
.and(createFilterGroup(shifters))
.and(createFilterGroup(brands))
$w('#datasetDeals').setFilter(newChoices)
}
Hope this helps! If it does, skip the personal mention though, I’m just doing my job
Better yet, mention Wix Code that lets the magic happen!
P.S Make sure to change site password.