I’m using a repeater connected to my database. I have a search bar and am using code from the video on “how to create a search for your database”. The problem I have is i want to be able to search for two elements from my database not just one. It can be one or the other not both together. For example, search ‘title’, or search a ‘category’. Here’s my code which only allows me to search for ‘title’ but not ‘category’. Thanks.
import wixData from “wix-data”;
export function searchBar_keyPress(event, $w) {
console.log($w(‘#searchBar’).value);
filter($w(‘#searchBar’).value);
}
function filter() {
$w(‘#dataset1’).setFilter(wixData.filter()
.contains(“title”, $w(“#searchBar”).value));
Hi Tal,
I greatly appreciate the reply and the solution to my problem. I have two concerns with the code you referenced in that’s post. The first is the use of drop downs, my ‘town’ element alone has close to 60 different types and growing. I don’t think that would be too practical on a drop down feature with that many. The second is I’ve tried a similar code to yours and found that it defaults to 50 results . When I added a .limit(10) for example it limited the result to 10 but when I hit next page to go the then next results it no longer filtered and just showed random 10 results.
Is it possible to use your code but have one search input and no drop downs, while searching for more than 1 element with a filter limit of let’s say only 10 per page being that the results are image heavy. Much appreciated thank you.
Ethan that worked perfectly thank you so much. Suppose I wanted to combine the search to allow by ‘title’ AND ‘category’ in one search, for example anything with “town A” and “Black” come up in the search query. any suggestions? thanks again
that’s fine i can do that, but how do i tie in a on click event and have an actual search button so its not an on keypress since that probably wont work with two search bars?
your awesome. it worked perfectly. and the search button removed the lag that onkeypress creates. I can’t thank you enough.
Suppose down the line I wanted one search input to handle multiple fields in the database would that even be possible or i’d always need either a drop down or an additional key input like you showed me just now?
also the onKeyPress lag can be reduced with a timer
let debounceTimer
function update() {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w('#searchBox').value);
}, 200);
}
export function searchBox_keyPress(event, $w) {
update();
}
function filter(title) {
//data search code with title holding the value of the searchbox
}
the 200 can be increased to any number. It causes the search to only happen X milliseconds after the user has stopped typing
let debounceTimer function update() { if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w(‘#searchBar’).value);
}, 200);
})
export function searchBox_keyPress(title, $w) {
function filter(title) { let totalResults = 5 // Number of results per page, default is 50 if (String($w(‘#PerPage’).value)) { totalResults = Number($w(‘#PerPage’).value ) } // requests per page dropdown