Hey guys,
i`m having trouble with my tags filter. My project is a new website for our animal shelter. The goal is that you can filter the gender, age and location so i created three different tag categories. The cat should show if at least one statement of all selected categories is true.
It all worked fine if you only select one tag of each category. It also works when only 1 or 2 categories are selected and one is empty, but as soon as you select multiple tags of one category no cat will be found.
I get that as no cat can be at two locations at the same time and i used filter.hasAll in my code. So i changed filter.hasAll to filter.hasSome and the problem was gone as i would get correct results even if i select every filter in one category.
However now there has to be a selection made on every category before a cat can be found.
How can i achieve that the filter starts working on the first selected tag and doesnt require a selection in every category before showing the result? My code is:
import wixData from 'wix-data';
$w.onReady(function () {
$w("#GeschlechtTags, #AlterTags, #AufenthaltTags").onChange(function () {
search();
});
function search() {
let filter = wixData.filter();
let geschlechtIdx = $w("#GeschlechtTags").selectedIndices;
let alterIdx = $w("#AlterTags").selectedIndices;
let aufenthaltIdx = $w("#AufenthaltTags").selectedIndices;
let geschlechtVal = $w("#GeschlechtTags").value;
let alterVal = $w("#AlterTags").value;
let aufenthaltVal = $w("#AufenthaltTags").value;
if (geschlechtIdx.length > 0 || alterIdx.length > 0 || aufenthaltIdx.length > 0) {
filter = filter.hasSome("tags", geschlechtVal)
.and(filter = filter.hasSome("alter", alterVal))
.and(filter = filter.hasSome("adresse", aufenthaltVal))
$w("#KatzenDataset").setFilter(filter)
.then(count)
} else {
$w("#KatzenDataset").setFilter(filter)
.then(count)
}
$w("#clearFilter").onClick(function () {
$w("#GeschlechtTags, #AlterTags, #AufenthaltTags").value = undefined;
$w("#KatzenDataset").setFilter(wixData.filter()).then(count);
});
}
//COUNT ITEM
function count() {
let count = $w("#KatzenDataset").getTotalCount();
if (count > 0) {
$w("#TrefferKatzen").text = `${count} Katze(n) gefunden`;
} else { $w("#TrefferKatzen").text = `Keine Katze gefunden`; }
return count;
}
$w("#KatzenDataset").onReady(function () {
count();
});
});
Thank you in advance