Filter search based on data in referenced collection

Hi,

I have a collection of ‘Owners’ and I have a collection of ‘Listings’. So each listing is connected to an owner. Some owners may have many listings. In the owners collection, one of the fields is ‘location’.

I have a category page that displays all listings in a repeater. I want users to be able to search the listings using a dropdown based on the owner of that listing’s location.

So if the code for the filter is:
$w(“#listings”).setFilter( wixData.filter()
.contains(“propertyname”, $w(‘#locationDropdown’).value))

How do I make the propertyname = the location of the listing’s owner?

I’ve searched the forums and I can’t work out whether this is possible or not? Is the only option to add the owner’s location as a field in the listing? This seems like it would be time consuming and very open to human error.

https://support.wix.com/en/article/displaying-content-from-multiple-database-collections-using-datasets https://support.wix.com/en/article/about-displaying-content-from-multiple-database-collections-using-datasets
https://support.wix.com/en/article/about-connecting-multiple-database-collections
https://support.wix.com/en/article/about-reference-fields-in-database-collections
https://support.wix.com/en/article/displaying-content-from-multiple-database-collections-using-reference-fields-4034931

the last link is probably the best one for you.

@givemeawhisky Thanks for replying. All those articles explain how to display filtered data using reference fields - but that’s not my issue. I’m wanting users to be able to use a dropdown to filter the results in a repeater based on information in a referenced collection.

export function locationDropdown_change(event) {
$w(" #listings ").setFilter( wixData.filter()
.contains(“propertyname”, $w(’ #locationDropdown ').value))}

So if this is the code for the dropdown - how do I make the propertyname = the location of the listing’s owner?

It says at the bottom of that last link.

Connecting Dropdown Elements to Reference Fields
You can connect a dropdown list element to a reference field. This means that the items in the dropdown list automatically come from the Main field in the referenced collection. If you do this on an input form, the value selected by your visitor gets saved to the reference field in your collection.

https://support.wix.com/en/article/adding-and-setting-up-a-dropdown-list

@givemeawhisky that’s not what I‘m trying to do. I’m trying to use the setFilter() function so that the user uses the dropdown to set a filter based on location. Lets say the drop down options are set as north, east, south and west, and the user selects the ‘North’ option. I then want to perform a search of the ‘listings‘ collection. In the listings collection, there’s a reference field called ’owner’ that connects each listing to an owner in the ’owners‘ collection. In the owner’s collection there’s a field called ‘location’ (values in that field will be either North, South, East or West) So I want the search to search the listings collection and return results of listings that are connected to an owner that have ‘North’ in their location field.

@thedressdirectory I know nothing about coding but maybe you can try what I did (image). Each reference field has a unique ID, and you can see that ID when you change your reference field to a plain text field (it will break connections and will show an error but it will display that unique ID) then you can change back.

@t8311834 Thanks for the suggestion! I’m not just wanting to filter by the text value in the reference field though, but rather a field in the collection that the reference field connects to. So say I have:

Listings collection
Fields - Name (Text field), Price (number field), owner(reference field connecting to owner’s collection)
Example listing - Name: Listing1, Price: 10, owner: (reference to) owner1

Owners collection
Fields - Name (Text field), email(text field), location(text field)
Example owner - Name: Owner1, email: owner1@email.com, location: North

So I want to filter the listings collection by the location field of the owner’s collection. So in the example, if I set a filter to show listings that have the location North - Listing1 will show in the repeater because it is connected to owner1 in the owner field, and owner1’s location is North. But if I set the location filter to South, listing1 wouldn’t show in the repeater because the owner it’s connected to is not located North.

I really hope I’m making sense - I feel like this is something simple but I can just not work it out. Any help would be appreciated! @givemeawhisky