I have a Text and a Search button on my home page. When user types a text to search and clicks on the search button on the home page , I would like to take him to a search page e.g /search page with the dataset dynamically set on a Repeater on this page . I have written below code on the OnClick event of the Search button (btnSearch) on the home page.
Below works if I am already on the /search page , but does not work if I am on the /home page. Any advise will be appreaciated.
Below is the code on the OnClick event of the Search (btnSearch) button on the home page:
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
export function btnSearch_click_1(event, $w) {
//Add your code for this event here:
Case 1)
When user enters search text on the HOME page and clicks on the search button , they get redirected to the search page, but the search does not execute automatically.
In my code example below code :
export function btnSearch_click_1(event, $w) {
//Add your code for this event here:
wixLocation.to(“/search”); -----> this executes correctly -----> Below does not execute
wixData.query(“DATABASENAME”)
.find()
.then( (results) => {
$w(“#repeater1”).data = results.items;
$w(“#txtSearchTitle”).text = “Test Search” ;
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Case 2)
When I am already on the SEARCH page , then everything works fine
Do we need any special code on the Onload event of the search page??
You’re right. You need to run the query in the onReady() function (that’s probably what you meant when you said Onload) of the search page. Right now you’re going to the search page, but then nothing triggers the update of the page - the right place for that is onReady().
let selectedCategory="HUM";
let keyword1="ERICA";
let keyword2="SOPHIA";
wixData.query('DATABASENAME')
.or(wixData.query('DATABASENAME').contains('title', selectedCategory))
.or(wixData.query('DATABASENAME').gt('someColumn', keyword1))
.or(wixData.query('DATABASENAME').lt('otherColumn', keyword2))
// add more .or's here
.find()
.then(res => {
console.log(res);
});
}
I am facing one more issue with the search functionality. I have a text and a search button on my home page. When the user types a text to search and clicks on the search button I excute below code:
export function btnSearch_click(event, $w) {
//Add your code for this event here:
if (wixLocation.path==“search”) {
Search($w(“#cboCategory”).value,$w(“#txtSearch”).value.trim(),$w(“#repeater1”));
}
else {
wixLocation.to(“/search”);
}
}
The search page has a repeater control connected to the dataset. The dataset is linked to a database with no filters. Below is the code in the Search page:
$w.onReady(function () {
//TODO: write your page related code here…
Search($w(“#cboCategory”).value,$w(“#txtSearch”).value.trim(),$w(“#repeater1”));
});
export function Search(selectedCategory,selectedSearch,SearchRepeaterData) {
// convert the keywords array to an array of queries (one for each keyword)
const allQueries = keywords.map(keyword => queryByKeyword(selectedCategory,keyword))
// join the different queries into one query using OR
const joinedQuery = joinQueriesWithOr(allQueries)
joinedQuery.find()
.then( (results) => {
console.log(joinedQuery);
SearchRepeaterData.data = results.items;
} )
.catch( (err) => {
let errorMsg = err;
} );
}
The issue I am facing is that when the search page loads it first executes the Dataset query (without filters) and then user has to click again to search on the search page. Is there a way to bypass the default dataset query?
Hi,
I’m still kinda new to using coding and stuff. I have a slight problem with a search code that I’m working on. I’m working with 3 Drop down User Input and a onClick Search button. And when I try and search, this is the error that I get " TypeError: Cannot read property ‘items’ of undefined "
Below is the coding that I’m working with.
import wixData from ‘wix-data’;
export function isearch_click(event, $w) {
$w(“#dataset1”).setFilter(wixData.filter()