Hi, I’m working on a property search and I can’t get the Collection Reference fields to filter the search. There are various dropdown fields (all with manually inserted values) which filter data in a repeater. Two of these fields need to filter a Reference field each.
So, I have a collection called ‘Properties’ which has all the information stored in it. Some of the fields are referenced to other collections – eg: ‘location’ field references to the ‘Localities’ Collection, and ‘propertyType’ field references to the ‘ProperType’ Collection.
I cannot get the Reference fields filter code to work. The other fields in the search filter work fine. I’m missing something and I cannot understand how to make it work. Can anyone please help?
This is the main database collection ‘Properties’ – the ‘location’ and ‘propertyType’ reference fields are in it:
This is one of the Reference database collection ‘Localities’. Field name: ‘locality’.
These are the search field to filter the database in the repeater:
This is part of the search/filter code (after you click on the Search button):
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import {session} from 'wix-storage';
$w.onReady(()=>{
$w('#datasetProperty').onReady(()=>{
init()
});
});
function init() {
filter();
///there's some more code between these lines
$w('#btnSearch').onClick(()=>{
filter();
});
}
async function filter() {
$w('#textResult').text = "Searching...";
let filterHolder = wixData.filter();
let inSearch = $w('#inSearch').value;
let dropMinSalePrice = $w('#dropMinSalePrice').value;
let dropMaxSalePrice = $w('#dropMaxSalePrice').value;
let propType = $w('#dropPropertyType').value;
let locationSelected = $w('#dropLocation').value;
let bedMin = $w('#dropMinBed').value;
let bedMax = $w('#dropMaxBed').value;
let bathMin = $w('#dropMinBath').value;
let bathMax = $w('#dropMaxBath').value;
let hasGarden = $w('#checkGarden').checked;
let hasPool = $w('#checkPool').checked;
let hasViews = $w('#checkViews').checked;
let hasGarage = $w('#checkGarage').checked;
let dropSaleRent = $w('#dropSaleRent').value;
if(hasGarden) {
filterHolder = filterHolder.eq("garden", true);
}
///there's some more code between these lines
///Property Type search <<< this doesn't work (database reference field)
if(propType && propType !== "Any") {
filterHolder = filterHolder.eq("propertyType", propType);
}
///Location search <<< this doesn't work (database reference field)
if(locationSelected && locationSelected !== "Any") {
filterHolder = filterHolder.eq("location", locationSelected);
}
console.log(filterHolder);
await $w('#datasetProperty').setFilter(filterHolder);
}
I’ve tried different methods but I can’t seem to filter the reference fields.