Hi,
I filter my database with a text input and a dropdown input (work together as AND filter) using the following code:
import wixData from “wix-data”;
$w.onReady(() => {
loadConstantsCategories();
showNextPageButtonIfNeeded();
});
let lastFilterTitle;
let lastFilterCategory;
let debounceTimerFilter;
let debounceTimerNextPage;
export function search_keyPress(event, $w) {
if (debounceTimerFilter) {
clearTimeout(debounceTimerFilter);
debounceTimerFilter = undefined;
}
debounceTimerFilter = setTimeout(() => {
filter($w(‘#searchInput’).value, lastFilterCategory);
}, 600);
}
export function category_change(event, $w) {
wixData.query(‘Cities’)
.eq(‘title’, $w(‘#dropdownCategory’).value)
.limit(1)
.find()
.then(res => {
if (res.items.length > 0) {
filter(lastFilterTitle, res.items[0]._id);
} else {
filter(lastFilterTitle, null );
}
});
}
function showNextPageButtonIfNeeded() {
// Show next page button
if ($w(‘#datasetCondos’).hasNextPage()) {
$w(‘#buttonLoadMore’).enable();
$w(‘#buttonLoadMore’).show();
} else {
$w(‘#buttonLoadMore’).disable();
$w(‘#buttonLoadMore’).hide();
}
}
function filter(title, category) {
if (lastFilterTitle !== title || lastFilterCategory !== category) {
let newFilter = wixData.filter();
if (title) {
newFilter = newFilter.contains(‘title’, title);
}
if (category) {
newFilter = newFilter.contains(‘city’, category);
}
$w(‘#datasetCondos’).setFilter(newFilter)
.then(() => {
// Show ‘Nothing found’ text if needed
let count = $w(“#datasetCondos”).getTotalCount();
count === 0 ? $w(‘#textNothingFound’).show() : $w(‘#textNothingFound’).hide();
showNextPageButtonIfNeeded();
lastFilterTitle = title;
lastFilterCategory = category;
});
}
}
function loadConstantsCategories() {
wixData.query(‘Condos’)
.include(‘city’)
.find()
.then(res => {
let categories = new Set();
res.items.map(item => {
return categories.add(item.city.title)
});
let options = [{“value”: ‘’, “label”: ‘All’}];
Array. from (categories).map(category => {
return options.push({“value”: category, “label”: category})
});
$w(‘#dropdownCategory’).options = options.sort();
});
}
If the dataset doesn’t contain the text from the text input, I get the error ‘An error occurred in one of currentIndexChanged callbacks DatasetError: could not resolve dynamic filter’ in the console, however everything works.
Thank you.