I would like to add a search function to my database, I have trawled the forum and managed to get so far in that one keyword searches the title column in my database and pulls up the relevant item. However, if I add two keywords that do not appear in the column next to each other no results are shown.
For example:
"Brook Mussel" pulls up Brook in mussel download links. However if I use "Brook J Handle" I get no results.
If someone could please point out where I have gone wrong in the code I would be very grateful!
import wixData from "wix-data";
$w.onReady(() => {
loadContinents();
});
let lastFilterTitle;
let lastFilterContinent;
let debounceTimer;
export function iTitle_keyPress(event, $w) {
let searchValue = $w('#iTitle').value;
let searchWords = searchValue.split(' ');
let query = wixData.query('#dataset1')
.hasSome('title', searchWords[0]);
for (let i=1; i < searchWords.length; i++) {
query = query.or(wixData.query('dataset1')
.hasSome('title', searchWords[i]));
}
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w('#iTitle').value, lastFilterContinent);
}, 500);
}
export function iContinent_change(event, $w) {
filter(lastFilterTitle, $w('#iContinent').value);
}
function filter(title, continent) {
if (lastFilterTitle !== title || lastFilterContinent !== continent) {
let newFilter = wixData.filter();
if (title)
newFilter = newFilter.contains('title', title);
if (continent)
newFilter = newFilter.contains('continent', continent);
$w('#dataset1').setFilter(newFilter);
lastFilterTitle = title;
lastFilterContinent = continent;
}
}
function loadContinents() {
wixData.query('Continents')
.find()
.then(res => {
let options = [{"value": '', "label": 'All Ranges'}];
options.push(...res.items.map(continent => {
return {"value": continent.title, "label": continent.title};
}));
$w('#iContinent').options = options;
});
}
Have you tried
.contains('title', searchWords[0]);