Filter results by reference field (.contains)

Hi !

I need help, I have a page that displays only certain items that contain the word “soldes” in the “evenement” field in the data collection, it worked well before, but I decided to set the “evenement” field as a reference, and now my code no longer works ! (no items are displayed on that page.

Here is a sample of the code :

//filters available/not available items
export function checkbox1_change(event, $w) {
	$w('#loader').show();
	let filter = wixData.filter();
	if ($w("#checkbox1").checked)
 	{
    	filter=filter.ne("dispo", true);
	}
	filter = filter
		.contains("title", $w("#searchInput").value)
		.contains("categorie", $w("#categorieInput").value)
		.contains("vendeur", $w("#vendeurInput").value)
		.contains("type", $w("#typeRadio").value)
		.contains("evenement", "soldes");
	$w("#dynamicDataset").setFilter(filter)
	//hides loading screen after data was retrieved
	 .then( () => {       
            $w('#loader').hide();
     });
}

any help as to what to do so the page only displays according to the reference field ?

I’ve found this but it doesn’t work :

Hi,

do you see those referenced fields displayed in the collection (that is values are blue as opposed to red)?

Hi,

I’ve found a compromise, i added each page the ID of the referenced field to the filter.

Can you show me your final code? I’m trying to filter by the reference field, but I guess wixcode won’t allow that?

Hi,

What I did was create a reference field in the collection. 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).

Copy that ID and paste it to the code with the .contains feature :

//searches keywords typed upon pressing 'Enter'
export function searchInput_keyPress(event, $w) {
	if (event.key === "Enter") {
	//shows a pre loader
		$w('#loader').show();
	let filter = wixData.filter();
	if ($w("#checkbox1").checked)
 	{
    	filter=filter.ne("dispo", true);
	}
	filter = filter
	//here is the ID linked to a reference in the collection
		.contains("evenement", "810fcbd1-becf-4471-8938-4a0c1afd38a0")
		//other criteria searched...
		.contains("title", $w("#searchInput").value)
		.contains("categorie", $w("#categorieInput").value)
		.contains("vendeur", $w("#vendeurInput").value)
		.contains("type", $w("#typeRadio").value);
	$w("#dynamicDataset").setFilter(filter)
	//hides loading screen after data was retrieved
	 .then( () => {       
            $w('#loader').hide();
            wixData.query("commentaires")
	  			.contains("reference", $w('#referenceText').text)
	  			.find()
		  			.then( (results) => {
						let numberOfItems = results.totalCount;
						$w('#nbrComments').text = numberOfItems + '';
					});
     });
	}
}