I need to filter a dataset based on a user’s search criteria. Specifically, I need compare what the user entered (critDesc) to a desc field and a photos field in the collection. If it is found in EITHER the desc or photos field then return the result. Here is my code:
$w(“#datasetBooks ”).setFilter( wixData.filter()
.contains(“desc”, critDesc)
.or(
wixData.query(“Library_Books”)
.contains(“photos”, critDesc)
)
Zero items are always returned no matter what the critDesc variable contains. So, how do I use an or condition with dataset setFilter?
J.D
January 5, 2020, 3:21pm
2
The second one is not a filter but a collection query.
If you want to filter the dataset, you should use wixData.filter().
Thanks. Now I need a little help with the syntax. This doesn’t work.
//this throws a TypeError: is not a function
$w(“#datasetBooks ”).setFilter( wixData.filter()
.contains("desc", critDesc)
.or(
$w("#datasetBooks").setFilter( wixData.filter()
.contains("photos", critDesc)
)
)
);
Note: if I simply use two .contains statements in the first filter, then the result is an “and” function instead of an “or” function.
$w(“#datasetBooks ”).setFilter( wixData.filter()
.contains("desc", critDesc)
.contains("photos", critDesc)
J.D
January 5, 2020, 5:49pm
4
Yes, you have a mistake there.
Try:
let filter1 = wixData.filter().contains("desc", critDesc);
let filter2 = wixData.filter().contains("photos", critDesc);
$w("#datasetBooks").setFilter(filter1.or(filter2));
(fixed)
This works!! Thanks J. D.