Hello!
I am having a problem getting aggregation, total count, and distinct total count results to update to reflect filter parameters.
-
I am running a query to populate a repeater with item data…it runs when the page loads…and it runs gain for each export function on-change event of dropdown & checkboxes.
-
I have set up a filter function, which alters query results based on the on-change events of the dropdown and checkboxes. See image for reference:
I have written 3 query export functions to get aggregation, total count, and distinct total count results.
const filter = wixData.filter().eq("showInListings", true)
export function Sum_units(){
wixData.aggregate("Portfolio")
.filter(filter)
.sum("units","sumamount")
.run()
.then( (results) => {
$w('#unitTotal').value = results.items[0].sumamount;
} );
}
export function Sum_projects(){
wixData.query("Portfolio")
.eq("showInListings", true)
.find()
.then( (results) => {
$w('#projectsTotal').value = results.totalCount;
} );
}
export function Sum_states(){
wixData.query("Portfolio")
.eq("showInListings", true)
.distinct("state")
.then( (results) => {
$w('#statesTotal').value = results.totalCount;
} );
}
When I activate the filter…with a dropdown change or checkbox change…the repeater results filter no problem…but the totals produced by the export query functions I created do not update to reflect the new totals relative to the filtered results.
Here is my filter function code:
function filterResults(results){
const state = $w('#dropdown1').value;
if (state && state !== 'All') {
results = results.filter(item => item.state === state);
}
if ($w('#checkbox1').checked !== false) {
results = results.filter(item => item.underConstruction === true);
}
if ($w('#checkbox2').checked !== false) {
results = results.filter(item => item.nowLeasing === true);
}
if ($w('#checkbox3').checked !== false) {
results = results.filter(item => item.managed === true);
}
if ($w('#checkbox4').checked !== false) {
results = results.filter(item => item.disposition === true);
}
return results;
}
Here is an example of the dropdown on-change event
export function dropdown1_change(event) {
//filtering the results
const filteredResults = filterResults(originalPropertiesInfo);
//setting the data
$w('#repeater1').data = filteredResults;
//setting the repeater elements
$w('#repeater1').onItemReady(($w, itemData) => {
$w('#thumb').src = itemData.thumbnail;
$w('#marketName').text = itemData.title;
$w('#location').text = itemData.location;
$w('#units').value = itemData.units;
I imagine that this is where I need to call the aggregation export functions…inside each onchange event.
This is the original query function that produces the results that are filtered
$w.onReady(async function () {
//Query to get the information from the database
wixData.query("Portfolio")
.limit(100)
.eq("showInListings", true)
.find()
.then((results) => {
originalPropertiesInfo = results.items;
$w('#repeater1').data = originalPropertiesInfo;
})
.catch((err) => {
let errorMsg = err;
});
// sums up total units, states and projects //
Sum_units();
Sum_projects();
Sum_states();
//Set the information to the repeater
$w('#repeater1').onItemReady(($w, itemData) => {
//add here all the relevant elements of the repeater
$w('#thumb').src = itemData.thumbnail;
$w('#marketname').text = itemData.title;
$w('#location').text = itemData.location;
$w('#units').value = itemData.units;
Looking at my code, I understand why it is not working…as I currently only run the 3 export query functions at the very beginning on page load to produce the tally and populate the repeater.
Is there a clean way to get my export queries to always draw from the filter results?
Any help here is greatly appreciated!! I feel like the answer could be quite simple.