Hello,
- I have 2 datasets: Review and Product
- Review has a reference on a Product
- Product has a Category
- I have repeater on my Review dataset.
- I would like to filter my reviews by product's category
I tried mostly everything. I added the Product dataset on my page and filter the review with "Same as" my Product dataset but it shows only one item.
Thanks for your help.
I find a way to do it
It's not elegant... but it works
1- I setup a dropdown with all my possible categories (loaded when page loads)
2- When the dropdown changed, I call my search() function
3- I added getFilteredProducts() function
function getFilteredProducts() {
let query = wixData.query('products');
// filter - add filter on product only if the dropdown element is not null or not all
if ($w('#dropdownCategory').value && $w('#dropdownCategory').value !== 'all') {
query = query.eq('category', $w('#dropdownCategory').value);
}
return query.find()
.then( (results) => {
return results;
}).catch( (err) => {
console.log(err);
});
}
4- In my search function I add the product filter with hasSome()
function search() {
//console.log('search');
let limit = 8;
if(wixWindow.formFactor === "Mobile") {limit = 4;}
// filter - search only for published reviews
let filter = wixData.filter().eq('isPublished', true);
if ($w('#dropdownCategory').value && $w('#dropdownCategory').value !== 'all') {
getFilteredProduits().then( (results) => {
let produits = [];
for (var i = 0; i < results.items.length; i++) {
let item = results.items[i];
produits.push(item._id);
}
filter = filter.hasSome('produit', produits);
getResults(filter, limit);
}).catch( (err) => {
console.log(err);
});
} else {
getResults(filter, limit);
}
}
function getResults(filter, limit) {
$w('#datasetItems').setFilter(filter);
$w('#datasetItems').getItems(0, limit);
}
Yisrael (Wix) helped me by this advice
" Your problem with access to the dataset is that the $w context selector of the onClick() function refers to the repeater item and does not have access to the page context. Therefore $w does not "see" the page's dataset... You need to change the context selector of your onClick() function...
I changed the context selector to $somethingelse, and then I access the dataset with the $w global context selector: $w(#dataset1" Another way would be to define the onClick() function in the onItemReady() function. You can see how that's done in the onItemReady() API."
Full text is here https://www.wix.com/code/home/forum/community-discussion/need-help-repeater-dataset
Good luck!
Hello, I tried that without any succes... I'll give it another try.
Thanks!
Hey. Maybe this article can help you.