How can I create "Refresh Button” to clear all the search filters?

I follow the instruction on this video, and added 4 dropdowns and a search button to filter dataset connected to a repeater.
https://www.youtube.com/watch?v=QhMKnm1f6EU
Once I use one of the dropdowns and press search button, it filters data properly , however…

PROBLEM
The repeater remains as it is filtered.
When I do a new search using any of the dropdowns, it doesn’t seem to filter “all data” but it filters “only what remains with previous search”.

I am making a job search page for jobseekers, so I expect users to do a new search again and again using different categories (ex. search by location, search by job type, search by industry) .

The code I use in my website for filter function.


import wixData from ‘wix-data’;

export function button18_click(event, $w) {
$w(“#dataset1”).setFilter( wixData.filter()
.contains(“JobType”, $w(‘#dropdown1’).value)
.contains(“Industry1”, $w(‘#dropdown2’).value)
.contains(“Location”,$w(‘#dropdown3’).value)
.contains(“Salary”,$w(‘#dropdown4’).value))

  .then((results) => { 
      console.log("Dataset is now filtered"); 
      $w("#repeater1").data = results.items;       
  }). catch ((err) => { 
      console.log(err);  
      $w("#repeater1").expand(); 

})

}


So, I want to add “Refresh Button” in the page, so that users can start fresh search. I already checked some existing forum on refresh function, but it was too difficult for me to understand… because, I’m not a coder. I only code when I need to add a function on my website. I usually just search copy and paste code.

Please help!

[@akawata] The way you remove a filter and reload all items in the repeater is to give the setFilter function an empty filter like this:

$w(“#dataset1”).setFilter(wixData.filter());

Et voilà!

Wow… it’s that simply…

$w(" #dataset1 ").setFilter(wixData.filter());
This made my “clear” button works exactly the way I want!!!

Thank you sooooo much, Steven!

Then, I’m facing another problem…(T_T)

Problem 1:
I have four dropdowns.
The very first search, filter works fine; if a user use only one dropdown, it filters the repeater properly. But, after pressing “clear” button, then search again, it no longer works like that; Nothing shows on the repeater, unless all four dropdowns choices matches…

Problem 2:
It would be nice if a user press “clear” button, all four dropdown boxes return to default placeholder text (i.e. a user search for “IT job”, then press “clear”, the dropdown returns to “search by Industry” instead of remain as “IT job”.)

Thank you in advance.

Dear Steven Cropper

Thank you so much for the message. (I was able to read your message that came into my mail box, but I can’t find it in this forum…)

For now, I give up on the 4 dropdowns.
I will use a simple search box that works just fine.

I rather want you to help me on this one.

When there is no matching data in the dataset after a user searches, I want to show “No result” message on the screen.

I wish the code for this is as simple as the previous one…

Thank you in advance.

@akawata

Regarding your problems…

  1. How to reset the dropdowns to default values:
$w(“#dataset1”).setFilter(wixData.filter())
.then(()=>{
    $w("#dropdown1").value = "";
     $w("#dropdown2").value = "";
     $w("#dropdown3").value = "";
     $w("#dropdown4").value = "";
})
  1. How to show the ‘No Results’ message on screen - add a normal text element and name it something like textNoResults. Then, in the section of the .then() function write a little bit of code to display that message is no results have been found like so:
$w(“#dataset1”).setFilter(wixData.filter()
    .contains("JobType", $w('#dropdown1').value)       
    .contains("Industry1", $w('#dropdown2').value)  
    .contains("Location",$w('#dropdown3').value)    
    .contains("Salary",$w('#dropdown4').value)
    ) 
    .then(()=>{     
    let count = $w("#dataset1").getTotalCount()
    if(count === 0){
        $w("#textNoResults").show();
        $w("#repeater1").hide();
    }else{
        $w("#textNoResults").hide();
        $w("#repeater1").data = results.items;  
})

Good luck!
Tiaan

@akawata

Following on from Steve’s great answer.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter

If you want to filter a repeater with dropdowns, then have a watch of this tutorial.
https://www.youtube.com/watch?v=r0DLqkRDJ34

Otherwise, have a look through these tutorials.
https://www.vorbly.com/Vorbly-Code/WIX-CODE-RESET-FORM-WITH-BUTTON
https://www.vorbly.com/Vorbly-Code/WIX-CODE-CLEAR-REPEATER-DATA
https://www.vorbly.com/Vorbly-Code/WIX-CODE-REPEATER-SORTING-BUTTON
https://www.vorbly.com/Vorbly-Code/WIX-REPEATER-MULTIPLE-CHECKBOX-FILTERS
https://www.vorbly.com/Vorbly-Code/WIX-REPEATER-MULTIPLE-FILTERS-WITH-RESET
https://www.vorbly.com/Vorbly-Code/WIX-REPEATER-WITH-FILTER-BUTTONS
https://www.vorbly.com/Vorbly-Code/WIX-REPEATER-WITH-MULTIPLE-FILTERS

Oh! Thanks Tiaan!

  1. How to reset the dropdowns to default values:
    This works sooo perfectly. I just copied and pasted, and it just worked!

  2. How to show the ‘No Results’ message on screen:
    I received errors when I copied and pasted the one you gave me…


I don’t know how to solve them…
Please help.
Thank you.

@givemeawhisky Thank you for your advice! These are too advanced for me (-_-)

@akawata There is a minor issue with the Forum code panel that posts the wrong code sometimes. The code that reads:

$w("#repeater"#repeater")

should read

$w("#repeater")

Cheers

@stevesoftwareservice Thank you so much Steven!
$w(“repeater”) works fine.

However, then, it shows another error at the very end of the line…


I feel like I am almost there but…

Hope anyone can still help me on this…

@akawata

Line 18 shows red dot which means you have error on that line, with the red line underlining where your error is in your code.

Simply delete the ‘)’ in your code and see what happens.

Basically you just need to make sure that in your code you have a matching number (pairs) of curly brackets and parentheses, which are the { and } and the ( and ) in your code.

Finally, where is your onReady call after your imports and before your export functions?
https://www.wix.com/corvid/reference/$w.html#onReady

@akawata @givemeawhisky Actually indentation of your code would help you a lot. The red dot is because your if/else conditional is not closed before closing the .then().

Your code should read (I have included indentations to show the help they give ;-)).

// the following code starts at your line 10 & inserts a “}” on a line above line 18.
.then(() => {
    let count = $w(“#dataset1”).getTotalCount();
    if (count === 0) {
        $w(“#textNoResults”).show();
        $w(“#repeater1”).hide();
    } else {
        $w(“#textNoResults”).hide();
        $w(”#repeater1”).data = results.items;
    } // <===== this is the missing line!
})

One other thing to note - you do not have a results variable anywhere so the assignment on line 17 will likely show your next red dot ;-). Since you probably have the dataset you are filtering bound to $w(“#repeater1”) there isn’t a need to assign a value to its .data value. The repeater should update when the filter completes. You should probably make line 17:

$w(“#repeater1”).show();

since you hide it in the if() part of your test you should show it in the else part.

Cheers

Thank you sooo much!
Your code works perfectly!!!
Now, all my problems have been solved. I couldn’t have done without your help!
Once again, thank you all!

}):
}

I want the same exact code to reset my selection tags unlike dropdowns but the code doesnt seem to work it shows multipe errors starting with .then.


starting with .then

I’m stuck with this too. Can anyone help? It clears the filters, but then the drop downs stop working. Any ideas?

import wixData from ‘wix-data’ ;

export function button14_click ( event ) {
$w ( “#dataset1” ). setFilter ( wixData . filter ())
. then (()=>{
$w ( “#dropdown1” ). selectedIndex = 0 ;
$w ( “#dropdown2” ). selectedIndex = 0 ;
})

}

@sara I normally use:

$w("#dropwdown1").value = undefined;
$w("#dropwdown2").value = undefined;

And that works for me.

@noahlovell Thanks Noah. I tried this but still not working.

https://hopscotchclient.wixsite.com/treebeard-v1/our-portfolio

Choose ‘Systems Change’ under All Themes

Hmmm. It might be to do with the fact that the dropdown are filtering the dataset via being connected to them? It might work better if they are filtering via code

@noahlovell Aha, yes. That could be it! I’m not a coder so I always try without code. But I have the code for the filters somewhere, I’ll try that. Thanks for the helpful hints, really useful to get someone else’s perspective!