Struggling with Repeater search code

Hi all,

I am new to this and am struggling with what is probably fairly simple code to setup a search for a Repeater that displays contents of a Dataset.

I’ve created the dataset and created a repeater that lists all the content/is connected. All I need now is a search box that searches the Name column of the dataset and displays the correct rows in the Repeater.

But - I’m struggling. I’ve read and watched many example but unfortunately can’t get my head around it.

Here is my code, can anyone please advise where I’m going wrong?

import wixData from ‘wix-data’ ;
export function searchbutton_click(event) {
wixData.query( “Grammar Schools” )
.contains( “title” , $w( “#searchbox” ).value)
.find()
.then(res => {
$w( “#repeater” ).rows = res.items;
});

}

To begin with try changing your repeater line to this.

 $w('#repeater').data = res.items;

Thank you I’ll do this right now

Or you could simply do it like this straight from a button click.

import wixData from "wix-data";

$w.onReady(function () {
$w("#searchButton").onClick(()=>{
$w("#dataset1").setFilter(wixData.filter()
.contains("title", $w("#searchbox").value));
});
});

That did seem to solve that error. However, now when previewing I see two errors:

Error parsing web-module ‘public/pages/masterPage.js’: Unexpected token (10:0) while parsing file: public/pages/masterPage.js

WDE0025: The Grammar Schools collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor.

The dataset set is called “Grammar Schools”, so I’m not realyl sure what’s happening here

@givemeawhisky Trying now!

@givemeawhisky I’m getting the error ‘setFilter’ does not exist on ‘#repeater’ on the example you listed

Your code should include the page onReady function as well.

Also check what your dataset is called in the Site Structure on the left hand side of your editor.

Is it Grammer School or is it GrammerSchool or is it Grammer_School etc.

import wixData from 'wix-data';

$w.onReady(function () {
});

export function searchbutton_click(event) {
wixData.query("GrammarSchools") 
  .contains("title", $w("#searchbox").value)
  .find() 
  .then(res => {    
    $w("#repeater").data = res.items; 
   });
}

For this code your dataset name should be the dataset id name that you have on your page.

Either hover over the dataset icon element or check the name in the properties panel.

import wixData from "wix-data";

$w.onReady(function () {
$w("#searchButton").onClick(()=>{
$w("#dataset1").setFilter(wixData.filter()
.contains("title", $w("#searchbox").value));
});
});

@nathanjohnfuller
@GOS said to setFilter on the dataset not the repeater, could that be the problem?

Kristof.

@givemeawhisky Thank you so much, it works perfectly. If I could squeeze one more piece of advice out of you, it woudl be how to amend the code so the UX us to hit Enter, rather than or as well clicking the Search button (as it’s more intuitive).

But - if you don’t have time for that I’m hugely grateful for your help, it has been a life saver.

@nathanjohnfuller

Just use onKeyPress on the user input that you are using for the search itself, along with a debounce so that you give the input a little extra time to catch all of the text used.
https://www.wix.com/corvid/reference/$w.TextInputMixin.html#onKeyPress
https://www.wix.com/corvid/reference/$w.KeyboardEvent.html

You can use it on the button still as shown in the first linked post below, however it will mostly always be put on the user input itself.

You can see previous posts about it here.
https://www.wix.com/corvid/forum/community-discussion/submit-button-activated-pressing-enter
https://www.wix.com/corvid/forum/community-discussion/input-onkeypress-function-working-only-after-clicking-away
https://www.wix.com/corvid/forum/tips-tutorials-examples/give-the-textinput-onkeypress-function-some-time