Hi,
I have 2 dropdowns which, when selected, return results from my datast into a repeater.
I am using an if/else statement to determine what happens when one or both dropdowns have a value and it is mostly working!
I’m just not sure whether it is necessary to repeat the count in each statement or if I can condense the code?
import wixData from 'wix-data';
$w.onReady(function ()
{
$w("#repeater1").data = [];
$w("#text14").hide();
$w("#repeater1").hide();
$w("#box1").hide();
$w("#button1").onClick(() => {
$w("#repeater1").data = [];
$w("#text14").hide();
$w("#repeater1").hide();
$w("#box1").hide();
if (($w("#dropdown1").value === '--All Regions--') && ($w("#dropdown2").value === '--All Specialisms--')) {
$w("#dataset1").setFilter(wixData.filter()
.eq("available", "1")
)
.then(() => {
console.log("Dataset is now filtered only by availability");
let count = $w("#dataset1").getTotalCount();
if (count > 0) {
$w("#box1").show();
$w("#repeater1").show();
} else {
$w("#box1").hide();
$w("#repeater1").hide();
$w("#text14").show();
}
})
.catch((err) => {
console.log(err);
});
} else if (($w("#dropdown1").value !== '--All Regions--') && ($w("#dropdown2").value !== '--All Specialisms--')) {
$w("#dataset1").setFilter(wixData.filter()
.eq("region", $w("#dropdown1").value)
.eq("specialism", $w("#dropdown2").value)
.eq("available", "1")
)
.then(() => {
console.log("Dataset is now filtered by region, specialism and availability");
let count = $w("#dataset1").getTotalCount();
if (count > 0) {
$w("#box1").show();
$w("#repeater1").show();
} else {
$w("#box1").hide();
$w("#repeater1").hide();
$w("#text14").show();
}
})
.catch((err) => {
console.log(err);
});
} else if (($w("#dropdown1").value) && ($w("#dropdown2").value === '--All Specialisms--')) {
//$w("#dropdown2").placeholder = '--All Specialisms--';
$w("#dataset1").setFilter(wixData.filter()
.eq("region", $w("#dropdown1").value)
.eq("available", "1")
)
.then(() => {
console.log("Dataset is now filtered by region and availability");
let count = $w("#dataset1").getTotalCount();
if (count > 0) {
$w("#box1").show();
$w("#repeater1").show();
} else {
$w("#box1").hide();
$w("#repeater1").hide();
$w("#text14").show();
}
})
.catch((err) => {
console.log(err);
});
} else if (($w("#dropdown1").value === '--All Regions--') && ($w("#dropdown2").value)) {
//$w("#dropdown1").placeholder = '--All Regions--';
$w("#dataset1").setFilter(wixData.filter()
.eq("specialism", $w("#dropdown2").value)
.eq("available", "1")
)
.then(() => {
console.log("Dataset is now filtered by specialism and availability");
let count = $w("#dataset1").getTotalCount();
if (count > 0) {
$w("#box1").show();
$w("#repeater1").show();
} else {
$w("#box1").hide();
$w("#repeater1").hide();
$w("#text14").show();
}
})
.catch((err) => {
console.log(err);
});
}
});
});
Thanks,
Rachel