hi,
1. I try to search the store database for two parameters.
In the name of the product and description of the product.
But the search result only shows me results from the name of the product and does not show products that the word I am looking for also appears in the description of the product.
2. I'm trying to make it that if you press ENTER on the keyboard it moves it to the search page.
the site: www.topink.co.il
import {local} from 'wix-storage'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; import wixWindow from 'wix-window'; $w.onReady(function () { $w('#repeater1').hide()
});
let lastFilterTitle;
let lastFilterDes;
let debounceTimer;
export function searchBox_keyPress(event, $w) {
$w('#repeater1').show()
$w('#searchBox').onKeyPress(event, $w) => {
let key = event.key;
}
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w('#searchBox').value);
}, 100);
}
function filter(title, des) {
if (lastFilterTitle !== title || lastFilterDes !== des) {
let newFilter = wixData.filter();
if(title)
newFilter = newFilter.contains('name', title);
if(des)
newFilter = newFilter.contains('description', des);
$w('#dataset1').setFilter(newFilter);
lastFilterTitle = title;
lastFilterDes = des;
}
}
export function searchButton_click() {
let word = $w("#searchBox").value;
local.setItem("searchWord", word);
wixLocation.to(`/results`);
}
it's still show only printer
like this
let lastFilterTitle; let lastFilterDes; let debounceTimer; export function searchBar_keyPress(event, $w) { $w('#repeater1').expand() if (debounceTimer) { clearTimeout(debounceTimer); debounceTimer = undefined; } debounceTimer = setTimeout(() => { filter($w('#searchBar').value); }, 100); } function filter(title, des) { if (lastFilterTitle !== title || lastFilterDes !== des) { let newFilter = wixData.filter(); if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.or(wixData.filter().contains('description', des)); $w('#dataset2').setFilter(newFilter); lastFilterTitle = title; lastFilterDes = des; } }
Right, exactly. If you want it to give you both you can use the following code instead.
if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.or(wixData.filter().contains('description', des));
the results that come up will be (1) printers with a title of 8250, and those with (2) a description of 8250?
this code--
if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.contains('description', des);
not give me the prodact with the 8250 in the description
https://www.topink.co.il/product-page/%D7%98%D7%95%D7%A0%D7%A8-%D7%9E%D7%A7%D7%A8%D7%95%D7%99-tn-321cl-brother
only the printer
https://www.topink.co.il/product-page/%D7%9E%D7%93%D7%A4%D7%A1%D7%AA-%D7%9C%D7%99%D7%99%D7%96%D7%A8-%D7%9E%D7%A9%D7%95%D7%9C%D7%91%D7%AA-%D7%91%D7%A8%D7%93%D7%A8-dcp-8250dn-brother
Topink,
Just to make sure, are you saying you want the filter to filter by title and description? Meaning that if I search for "8250", the results that come up will be (1) printers with a title of 8250, and those with (2) a title and description of 8250?
If so, that's exactly what your code here does correctly. It adds both filters.
if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.contains('description', des);
But, if you're saying you want it to have the option of (3) printers with just a description of "8250", you'd need to change your code a little bit, or use the or function of the WixDataFilter
On your page, you need to have a title text component and description text component. For an item as a query result, just set these components to item.title and item.description.
Ok, how do I do this in code? Because I only managed one parameter
If you search for printer 8250, then the query will return that record, including the name and the description.
I want one search line to look for both the name of the product and the description of the product.
I have a website for printers and printer ink.
When a customer searches for a sample printer 8250. There is a printer with that name and the 8250 cartridge appears in its description. So I want them both to appear in the results
"when searching for a product will search both parameters" - That's what the example does. You're right, I don't understand.
I think you do not understand me, I made like in the video but without the option of choice. I want to add when searching for a product will search both parameters the name of the product and the description of the product and will show what he found in the description and name.
i try this
function filter(title, des) { if (lastFilterTitle !== title || lastFilterDes !== des) { let newFilter = wixData.filter(); if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.contains('description', des); $w('#dataset2').setFilter(newFilter); lastFilterTitle = title; lastFilterDes = des; }
and this
function filter(title, des) { if (lastFilterTitle !== title && lastFilterDes !== des) { let newFilter = wixData.filter(); if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.contains('description', des); $w('#dataset2').setFilter(newFilter); lastFilterTitle = title; lastFilterDes = des; } }
and this
function search() { wixData.query("Stores/Products") .contains('name', $w("#searchBox").value) //.contains('description', $w("#searchBox").value) .find() .then(res => { $w('#searchRepeater').data = res.items; }) .catch(err => { console.log('err', err) }) }
I'm not an expert. I make every possible attempt and sometimes it succeeds and sometimes I do not. I try to work according to logic. But my logic and the logic of a code expert are different, so I turn here for help.
I did most of the way and I just needed the final touch to complete what I needed.
What isn't working? Take a look at Example: Search a Database which is the code to the tutorial article that you used, and does exactly what you want. You can load this example in the editor, try it, and modify it to your own use.
let lastFilterTitle; let lastFilterDes; let debounceTimer; export function searchBar_keyPress(event, $w) { $w('#repeater1').show() if (debounceTimer) { clearTimeout(debounceTimer); debounceTimer = undefined; } debounceTimer = setTimeout(() => { filter($w('#searchBar').value); }, 100); } function filter(title, des) { if (lastFilterTitle !== title && lastFilterDes !== des) { ///// i try search in name and in description let newFilter = wixData.filter(); if(title) newFilter = newFilter.contains('name', title); if(des) newFilter = newFilter.contains('description', des); $w('#dataset2').setFilter(newFilter); lastFilterTitle = title; lastFilterDes = des; } }
Where in your code is the title to be used in the filter? Where in your code is the description to be used in the filter? You need to call the filter function with both the title and the description.
ok how i do that i am not an expert
Your filter function has two parameter:
function filter(title, des) {
However, you are only calling it with one parameter:
filter($w('#searchBar').value);
If you want the search also based on description, then you will need to pass both items to the filter function.
on site header and on page rsults. www.topink.co.il
What page is this code on?
It is properly connected.
I try to do when pressing the ENTER key on the keyboard when in SEARCHBOX. This puts you to the results page
These following lines of code are not just unnecessary, but it probably overrides the searchBox_keyPress() event handler so it most likely never gets called. And that will most likely prevent your word search from working.
$w('#searchBox').onKeyPress(event, $w) => { let key = event.key; }
You should first delete those lines of code, and make sure that the searchBox_keyPress() is connected in the input field's property panel.