Dear community
I've created a "property search" using dropdowns working fine, but since i add another filter named "balcony" using checkbox" (#checkbox1) when i run the script, i have always "NO RESULTS" unless i check my checkbox, so i got results with balcony, but if i don't "check" my box : NO RESULTS. here is my code :
import {wixData} from 'wix-data';
export function button1_click(event, $w) {
$w("#dataset1").setFilter(wixData.filter()
.contains("nomDuBien", $w('#dropdown5') .value)
.ge("pieces", $w('#dropdown2') .value)
.eq("balcon", $w('#checkbox1').checked)
.between("surface",parseFloat($w('#dropdown6').value), parseFloat($w('#dropdown7').value))
.between("price",parseFloat($w('#dropdown3').value), parseFloat($w('#dropdown4').value)))
.then((results) => {
if ($w("#dataset1").getTotalCount() > 0) {
$w("#repeater1").expand();
$w("#text80").text = "Voici vos résultats";
$w("#repeater1").data = results.items;
} else {
$w("#repeater1").collapse();
$w("#text80").text = "Désolé il n'y a pas de résultats, essayez d'autres critères";
}
}).catch((err) => {
console.log(err);
});
}
I need to understand how to get results even if the (#checkbox1) is not checked, i still got my results. Thx to all...
This code below is working smoothly thanks to @Yisrael (Wix) ! i even created another checkbox "#checkbox2" for sea view... ans i can add as many filter as i want now ! thanks again...
let filter = wixData.filter() .contains("nomDuBien", $w('#dropdown5') .value) .ge("pieces", $w('#dropdown2') .value) .between("surface",parseFloat($w('#dropdown6').value), parseFloat($w('#dropdown7').value)) .between("price",parseFloat($w('#dropdown3').value), parseFloat($w('#dropdown4').value)) if($w('#checkbox1').checked === true) { filter = filter.eq("balcon", true); } if($w('#checkbox2').checked === true) { filter = filter.eq("vuemer", true); } $w("#dataset1").setFilter(filter)
Your filter:
.eq("balcon", $w('#checkbox1').checked)
inspects the value of the balcon field.
If the balcon field is true, then it's successful. But what happens if the field is not true. Is the field set to false, or is it undefined? If the balcon field is undefined, that means it's not equal to false (and of course it's not equal to true either).
Also, if the case when #checkbox1 is not checked means that you don't want to apply a balcony filter, then you need to ignore this filter.
You can build your filter [something] like this:
let filter = wixData.filter() .contains("nomDuBien", $w('#dropdown5') .value) .ge("pieces", $w('#dropdown2') .value) .between("surface",parseFloat($w('#dropdown6').value),parseFloat($w('#dropdown7').value)) .between("price",parseFloat($w('#dropdown3').value),parseFloat($w('#dropdown4').value)); // see if we want to add the balcony filter if($w('#checkbox1').checked === true) { filter = filter.eq("balcon", true); } $w("#dataset1").setFilter(filter);
Note: this has not been tested. It merely illustrates how you can conditionally include certain conditions to a filter.