Good day. I am Russian, I don’t speak English at all. I use google translator :))
Thanks to the moderators, I found some things that I was very looking for.
But here is my code, where the database is searched for by any of the entered words by pressing the Enter key.
I don’t understand how to add the ability to search with the click of a button in this code?
import wixData from 'wix-data';
let searchWords, inputString, lastFilterTitle;
$w.onReady(function () {
$w("#pole").onKeyPress((event) => {
if(event.key === "Enter") {
inputString = $w("#pole").value;
searchWords = inputString.split(" ");
runFilter(searchWords);
};
function runFilter(wordArray){
let filterSetting = wixData.filter();
for (let i=0; i < wordArray.length; i++)
{
filterSetting = filterSetting.contains('tovar', wordArray[i]);
}
if (lastFilterTitle !== inputString) {
$w('#dataset1').setFilter(filterSetting);
lastFilterTitle = inputString;
}
}
})
});
I tried different options, but nothing worked. Thanks in advance for your help. Have a nice day
$w.onReady( function () {
function myCode() {
//input code here to be executed when button is pressed or when the enter key is pressed
console.log("code running")
}
$w(‘#input1’).onKeyPress((event, $w) => {
if (event.key === “Enter”)
{
myCode();
}
})
$w('#button1').onClick((event) => {
myCode();
})
})
Does not work.
How exactly should I insert this code?
It’s quite simple, you just enter the code you want to run into where it says " input code here to be executed when button is pressed or when the enter key is pressed ".
You will end up with something like this…
import wixData from ‘wix-data’;
let searchWords, inputString, lastFilterTitle;
$w.onReady( function () {
function myCode() {
//input code here to be executed when button is pressed or when the enter key is pressed
console.log("code running")
inputString = $w("#pole").value;
searchWords = inputString.split(" ");
runFilter(searchWords);
function runFilter(wordArray){
let filterSetting = wixData.filter();
for ( let i=0; i < wordArray.length; i++)
{
filterSetting = filterSetting.contains(‘tovar’, wordArray[i]);
}
if (lastFilterTitle !== inputString) {
$w(‘#dataset1’).setFilter(filterSetting);
lastFilterTitle = inputString;
}
}
}
$w(‘#pole’).onKeyPress((event, $w) => {
if (event.key === “Enter”)
{
myCode();
}
})
//add a button to your page and give it the ID button1
$w(‘#button1’).onMouseIn((event) => {
myCode();
})
})
It works! Thank you very much! It was very important for my project. I didn’t insert the code correctly. I will try to understand better.