Dataset Filter Resetting After User Search

Hi everyone!
Very new to Wix, so forgive me if this is an easy fix.

Setup: repeater connected to dataset with filter. The page also has a user input I’m using as a search bar. As the user inputs text, the filtered dataset refreshes and shows content that contains the words being searched.

Issue: if the user clears the text in the search bar, the page resets to a repeater that is not filtered and the user sees all dataset entries. So I’m looking for a way to set it so once the user clears the text in the search bar, it goes back to the original filtered repeater they saw initially.

Thank you for your help!

Code below:

import wixData from ‘wix-data’;

let debounceTimer;
export function searchBar_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(()=>{
filter($w(‘#searchBar’).value);
},200)
}
let lastfilterTitle;
function filter(title){
if (lastfilterTitle !== title) {
$w(‘#dataset1’).setFilter(wixData.filter().contains(‘title’, title));
}
}

just use $w(’ #dataset1 ').setFilter() to cleae it

1 Like

Thanks Andreas! I don’t think that’s exactly what I want or I’m not using it correctly. When the page loads, the dataset already has a filter set. I want the page to return to that initial filter once a user clears the text they input into the search bar.

@Andreas Kviby Having same exact issue. Filters are set up via code for the search bar and drop down. This does not play well with the dataset filter set up by manage filter. Gotta be a workaround?!

Will, if you are using only the search bar and not a second search in combination, I believe that Andreas’ fix is what you need.

Mine is search bar and drop down:
import wixData from “wix-data”;

$w.onReady(() => {
loadTactical();
});

let lastFilterReference;
let lastFilterlistingGroup;
let debounceTimer;

export function iTitle_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w(‘#iTitle’).value, lastFilterlistingGroup);
}, 500);
}

export function iCategory_change_1(event) {
filter(lastFilterReference, $w(‘#iCategory’).value);
}

function filter(title, category) {
if (lastFilterReference !== title || lastFilterlistingGroup !== category) {
let newFilter = wixData.filter();
if (title)
newFilter = newFilter.contains(‘reference’, title);
if (category)
newFilter = newFilter.contains(‘listingGroup’, category);
$w(‘#dataset1’).setFilter(newFilter);
lastFilterReference = title;
lastFilterlistingGroup = category;
}
}

function loadTactical() {
wixData.query(‘Tactical’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All Categories’}];
options.push(…res.items.map(category => {
return {“value”: category.title, “label”: category.title};
}));
$w(‘#iCategory’).options = options;
});

}

export function iTitle_change(event) {
//Add your code for this event here:
}

Hi Jeff did you ever get your reset filters to work… my drops downs work, but I cant for my text box?