I’ve just started using Wix Code, and I confess I’m still trying to find my feet here.
I’ve set up a database to house a list of literacy programs, and I created a dataset and linked that to a repeater on my page. Because people will be searching for a program near them, I’ve added a drop down box where people can choose their city, and then the list filters based on that.
So far I have the drop down list updating the filter with the “city” field:
export function filterInput_change(event, $w) {
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“city”, $w(“#filterInput”).value));
}
But, I cannot for the life of me figure out how to make it so that it will clear the filter and show all cities again.
Any help would be appreciated.
Many thanks!
Wanda
Just use setFilter() and it should clear it.
export function filterInput_change(event, $w) {
$w("#dataset1").setFilter();
}
Hi Andreas,
Thanks for the suggestion - I tried it, but either I didn’t do it right, or it doesn’t work…
I was a bit confused by your code, because (to me) it looks like it’s telling the drop down to clear the filter when someone chooses a different entry… but that conflicts with my first bit of code telling it to change the filter to the new input.
So, instead I tried adding a button that the user can click to reset the filter, and put your suggestion there:
export function clear_click(event, $w) {
$w(“#dataset1”).setFilter();
}
Unfortunately, this didn’t work. Was it supposed to? Did I miss something?
After some playing, I managed to get something working. Figured I’d post it here, in case it’s of use to anyone else. This is my full final code:
import wixData from ‘wix-data’;
export function filterInput_change(event, $w) {
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“city”, $w(“#filterInput”).value));
}
export function clear_click(event, $w) {
$w(“#filterInput”).selectedIndex = 0;
$w(“#dataset1”).setFilter(wixData.filter());
}
I had to add an extra entry to the drop down so that the first item is “Choose a city” - that way once the filter is reset, the drop down returns to the first item which also tells the user what to do.
It works fine - although in the preview it seems to take a second to take effect… if anyone knows how to make it work with less of a delay, I’d love to know! In the meantime, it’s functional and I’m happy!
Oh shit, sorry. I did not read the functions name. Yes you should have that inside a clear function. There is another thread that solved your problem so I guess insetad of writing it again, look here: