Display aggregate total in text

I have a page that displays all of my Budgets and have successfully aggregated the item amounts for the dataset. Unfortunately I am having difficulty displaying the result in Text at the bottom of the page. Here’s what I have so far…

import wixData from ‘wix-data’;
$w.onReady( function () {
//REPEATER CALCULATED FIELDS
$w(‘#ApprovedBudgetDataset’).onReady( ()=> {
$w(‘#repeater1’).forEachItem(updateRepeaterItem);
});
//AGGREGATIONS
let filter = wixData.filter().eq(“Status”, “Approved”);
wixData.aggregate(“Budgets”)
.filter(filter)
.limit(1000)
.sum(“ApprovedItemAmount”)
.run()
.then( (results) => {
let TotalItemAmount = results.items[0];
console.log(TotalItemAmount);
$w(‘#TotalItemAmount’).text = results.items[0].toString();
});
});
The console confirms the result is correct, and I believe I need a Return in there somewhere. Any guidance would be greatly appreciated.

James,

It looks like you just need to make one little tweak to the assignment line by specifying the summed field name in the item. It’s probably always the same name for a sum: totalSum. To verify, click on the three dots in the console.log. Here’s what mine says:

$w(’ #TotalItemAmount ').text = results.items[0].totalSum.toString();

Thank you!!! That worked!!

Hello, I am having a similar problem. @tony-brunsman Do you know what I am doing wrong, or how I can fix this?

I cannot get the result to display in the input (input7).

export function button12_click(event) {
const filter = wixData.filter()
.eq(“yard”, $w(“#yarddropdown”).value)
.eq(“month”, $w(“#datedropdown”).value)
.eq(“email”, $w(“#input3”).value)
wixData.aggregate(“member-File”)
.filter(filter)
.sum(“countItems”)
.run()
.then( (results) => {
if (results.items.length > 0) {
let items = results.items;
console.log(results.items)
$w(‘#input7’).value = results.items
} } );
}

Here is the result I am seeing…the number is correct, but I am stuck trying to get input 7 to display that value.

Thank you

$w(’ #input7 ').value = results.items(0).countItemsSum

Thank you for the response @tony-brunsman . Unfortunately I am still have the same issue where the value is not displayed in the input element on the page. Is there something else that could be preventing the value to be displayed?

@cale Somehow, the element name got duplicated when I copied and pasted it. This is how it should be:

$w(’ #input7 ').value = results.items(0).countItemsSum;

Happened again when I published. Just remove one of the “#input7” references.

@tony-brunsman Thank you. I cannot get it to work. Should I be putting this code on the page code, or somewhere else? does the input element need to be connected? Not sure what else to try. I appreciate your help.

@cale Having it in the button click event is fine. The data is loaded at that point, so it should work. Are you still seeing the same thing in the console?

@tony-brunsman Yes I still see the same thing in the console. Thanks for replying.

@cale Could you post a screenshot of the current code?

@tony-brunsman

@cale My apologies. Brackets should be used around the 0:

results.items[0].countItemsSum;

@tony-brunsman It works! Thank you so much for your help!