Unable to set filter on a repeater based on a Reference field

Hello !
So what I’m trying to do it to display element in a repeater that match the same “category” (reference field in the database) as the item currently displayed.

It won’t let me select the dataset in the menu (see screenshot)

I tried this bit of code to apply the filter on the dataset but it doesn’t work :

// 'goodies' is the database collection
//'collection' is the reference field that is set in the goodies database collection
//button31 displays the name of the collection the current item belongs to
let filter = wixData.filter();
filter = filter
.contains("collection", $w("#button31").label);
$w("#dataset4").setFilter(filter);
wixData.query("goodies")
.contains("collection", $w("#button31").label)
.find()
.then( (results) => {
let numberOfItems = results.totalCount;
// collapses the 'goodies smilaires' box if there are no results
if (numberOfItems <=1 ) $w("#box47").collapse;
});

I also want the repeater to collapse if there are none items to display.

So is that a bug or did I do something wrong here? Many thanks!

Hi Tristan!

Please check out this article (" Displaying Filtered Information (Master-Detail) " section) to see if everything is set up correctly in your collections: https://support.wix.com/en/article/about-displaying-content-from-multiple-database-collections-using-datasets

This article also may be useful: https://support.wix.com/en/article/displaying-content-from-multiple-database-collections-using-datasets

If you have checked everything and don’t see the issue, then please send us a link to your site, so we can take a look at it.

Thanks for the answer. I’ve checked everything and couldn’t find why it doesn’t work.

I want to show a list of items that belong to the same category (called " collection " in the database), as the item that is currently displayed
I have created a dataset called “goodies-similaires” (similar goodies) to which each item in that list is connected.

I have created another dataset connected to the " collection" database. This dataset won’t let me filter objects that belong to the same collection field as the current item that is displayed.


(you can see that the “collection” field is greyed).

In short, I’m trying to display suggested content (same as on Amazon or any other website) that belongs to the same category (called here “collection”) as the current item that is browsed.

For example, that one is a set of Crash Bandicoot goodies.
https://www.wehaveadeal.net/goodies/1561119200

I want that list to show all other 3 Crash Bandicoot related goodies that I have in my database.
(You can’t see the “suggestion” box as I have hidden it at the moment)

Hi !
So here’s an update on the matter.

I managed to set the filter so the repeater show only items that belong to the same collection (reference field).
But that involves a (crappy) workaround.
I have added a button (#button63) (which will need to be hidden from the user…) that is linked to the collection ID of the current item.

Then I added this bit of code :

$w("#dataset4").onReady( () => {
//shows only items related to the current item
wixData.query("goodies");
let filter = wixData.filter();
filter = filter
.hasAll("collection", $w("#button63").label);
$w("#dataset4").setFilter(filter);

So until I find a more proper way to do this, I’m gonna stick to this…

But I still can’t seem to have the box collapsed if the repeater returns 0 or 1 item…

I tried that :

let count = $w("#dataset4").getTotalCount()
.hasAll("collection", $w("#button63").label);
if (count <=1) $w("#box47").collapse;

but I first tried that :

wixData.query("goodies")
.hasAll("collection", $w("#button63").label)
.find()
.then( (results) => {
let numberOfItems = results.totalCount;
// hides the 'goodies smilaires' box if there are no results
if (numberOfItems <=1 ) $w("#box47").collapse;
});

Strange because I have a similar bit of code that works.

wixData.query("commentaires")
.contains("reference", $w('#input2').value)
.find()
.then( (results) => {
let numberOfItems = results.totalCount;
// set plural / singular versions of number of comments
if (numberOfItems <=1 ) $w("#text96").text = numberOfItems + ' COMMENTAIRE';
else $w("#text96").text = numberOfItems + ' COMMENTAIRES';
if (numberOfItems >=2 ) $w("#commentsDropdown").show();
if (numberOfItems >=20 ) $w("#pagination1").show;
});

The above code works.

Any idea why???

bump !

Probably because the second one is using the contains query function and so it is only looking for
something that matches the users input inside your collection which could be in different combinations, say like for text in a collection of tea products and you look for tea so could return tea from teabag, teacup, teapot, etc, etc,
contains( ) - Refines a query or filter to match items whose specified property value contains a specified string.

Whereas the first one you are using the hasAll query function and so it is only looking for something that matches the button value exactly.
hasAll( ) - Refines a query or filter to match items whose specified property values equals all of the specified value parameters.

As you are looking for an exact match with hasAll it will either be a yes or a no, whereas your contains could or will be a variable number, so simply try changing the less than 1 part to something similar to this which I have just pulled from code on a page where I have used similar myself where if the results is 0 etc.

.then( (results) => {
// if an item for the user is not found
if(results.items.length === 0) {

You will be able to see an example of this if you look in the Wix API Reference for the Data Query and the hasAll function itself where they have done it if the results are greater than 0.

Create a query, add a has all filter, and run it

import wixData from 'wix-data';

// ...

wixData.query("myCollection")
  .hasAll("colors", ["red", "yellow", "blue"])
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
    // rest of code from the example//

Also, with regards to your reference field, that should not be a problem if you are filtering the database that the reference field is in.

Plus Wix already do a Related Items tutorial for in the Wix Store, so have a look at it if not done so already and see if you could use it or change it to suit your own page.

https://support.wix.com/en/article/corvid-tutorial-adding-a-related-products-area-to-a-wix-store-product-page
https://www.youtube.com/watch?v=KgqPvljpbSo - Wix YouTube video of tutorial