Display Sum of Fields

I have a repeat region that lists a set of records. One of the fields is a dollar amount. I want to display the SUM of that field.

How do I bind it to the page properly? When I bind it I get the following code which of course only shows the first record amount.

<p class="text-right font-weight-bold"> ${{SkillsHighwayTrainingCosts.data.query1[0].TotalTraining}}</p>

Here is some visuals:

Hello Brad,
On the page, where you want to bind the sum, select your query, then format data, then select sum and enter the property (db field name) which values you want to sum :slight_smile:

Beautiful and easy! Thanks!

Now I have another issue … drops the trailing 0 in the total.

Screen Shot 2020-01-22 at 1.16.07 PM

Maybe you just want to look at the numbers formatting options :slight_smile:

I thought of that but it doesn’t seem to make a difference.

And if I add format currency I get $NaN as a total

I was not referring to the to number formatter, it just converts a string to a number.
I am referring to the format number formatter, available under numeric menu. There you can specify the number of decimals.

The Numeric menu isn’t available unless I convert to number first. Could it be the field type? I have the field type as decimal in the database?

What if you add the format number after to number?

${{SkillsHighwayTrainingCosts.data.query1.sum(`cost_amount.toNumber().formatNumber(2, ".", ",")`)}}

I get NaN displayed.

Your expression is wrong Brad, in the data picker you need to right click and add the formatters not to the property, but one level up, to the sum. So you should end up with something like:

${{SkillsHighwayTrainingCosts.data.query1.sum(`cost_amount`).formatNumber(2, ".", ",")}}

Ok, your code worked. I will have to figure out what I was doing wrong with my selections.

Thanks again for the amazing support!

Edit: Okay, I see where I was going wrong. Makes sense once you see it done right.

You want to format the sum, not the property you are summing :slight_smile:
In this window:

Right click the Sum, not its property and then apply the format number option.

1 Like

Brad, There is something not quite right, Wappler should show the formatNumber thingy without have to convert to number. This problem started a couple of months ago when the formatter was rehashed.

2 Likes

Thanks Ben. I wondered about that.