Search Results 'no results found' text to display

Hi,

I have been trying to get some text to display when there isn’t any results returned from a search, it works but is delayed by one ‘onClick’ of the search button. So if the results returns no results, the text doesn’t display, but if you click the button again, it displays. The code I have is below and if anyone could help me it would be much appreciated.

function filter(searchBox, coursesCategory, duration, startDate, difficulty, region, dates) {
 let newFilter = wixData.filter();
 if (searchBox) newFilter = newFilter.contains('title', searchBox);
 if (coursesCategory) newFilter = newFilter.contains('coursesCategory', coursesCategory);
 if (duration) newFilter = newFilter.contains('duration', duration);
 if (startDate) newFilter = newFilter.eq('dateone', startDate).or(newFilter.eq('dateTwo', startDate).or(newFilter.eq('datethree', startDate).or(newFilter.eq('dateFour', startDate).or(newFilter.eq('dateFive', startDate).or(newFilter.eq('dateSix', startDate))))));
 if (difficulty) newFilter = newFilter.contains('pageTitle', difficulty);
 if (region) newFilter = newFilter.contains('region', region);
 if (dates) newFilter = newFilter.contains('dates', dates);
        console.log(searchBox);
        console.log(coursesCategory);
        console.log(duration);
        console.log(startDate);
        console.log(difficulty);
        $w('#coursesDataset').setFilter(newFilter);
        lastFilterSearchBox = searchBox;
 if( $w('#coursesDataset').getTotalCount()===0 ) {
            $w("#NoResultsText").show();
            }
 else {
         $w("#NoResultsText").hide();
        }
 
}

export function SearchButton_click(event, $w, res ) {
    filter($w('#iTitle').value, $w("#coursenamedropdown").value, $w("#durationdropdown").value, 
    $w("#Coursestartdate").value, $w("#dropdownDifficulty").value, $w("#regionDropdown").value, $w("#CourseAccomodationDropdown").value);

}

Anyone got any ideas?? I’ve been through the code and can’t seem to work out why this happens…

Hi,
I recommend following the example here to learn how to write a filtering repeater function. Than, you can have a text box hidden with an error message. If after calling the filteredResults function, it returns an empty array ( filteredResults.length === 0 ), you can show the error message you’ve added.

I hope it’s clear.

Best,
Tal.

Hi Tal,

Thanks for getting back to me, I have been researching and managed to get the following code to do the job under the above function.

 
 $w('#coursesDataset').setFilter(newFilter).then(() => {
 let textVariable = $w('#coursesDataset').getTotalCount();
        $w('#resultsText').text = `${textVariable} courses found.` 
        lastFilterSearchBox = searchBox;
})