How to SUM values in a repeater column

How do I SUM the values in a repeater column?

I have a repeater with a price column. Everytime I add a new price I want to update a text widget with the SUM of all the values in the price column.


You can do this in the OnItemLoad of the Repeater:
“Set value of Sum equal to [[Sum + item.Price]]”

You will also have to make sure to clear this when adding a new item. In AddItem’s OnClick:
“Set value of Sum equal to 0”

See It Live: Repeater Sum Row Example
RP File: SumRepeaterRow.rp (69.1 KB)

**A couple extra notes. I am setting the total value OnItemLoad as well, this means it sets it to all the intermediate values as well, but it doesn’t matter because the last value set is correct. You could choose to use the total [[Sum]] value at another place though.

Also, watch out for other Actions that will make the Repeater refresh (e.g. setting a filter, deleting rows). This will call OnItemLoad for all the Repeater Items again and if you do not clear your [[Sum]] to 0 first in those Actions you will end up doubling and tripling, etc… it.



Now even simpler question: how to find the max value using Math.max(x.y)?


Sorry. It might be stupid question but what does the SUM global variable contain? (Is there a way to edit global variables?)

Thank you, Judit

Sum contains the total of the right column of the Repeater.

Edit Global Variables at 'Project Settings -> Global Variables.

You could do something similar, just replace:
“Set value of Sum equal to [[Sum + item.Price]]”

“Set value of Max equal to [[Math.max(Max, item.Price)]]”

What if the Add Button were on a separate page how would you prevent the onload item from firing when you go to the page and then your calculation ends up double plus your new value?

This was specific to this example, but if you do anything else that causes the Repeater’s Items to refresh you will need to set your Sum variable to 0.

Hi Ian,

I opened the .rp example and tried it on a file of mine also. I’m unable to understand how the Sum variable adds up items in a column because it’s self-referencing : Sum = Sum + Item.Price. Where in here do we have the formula adding up all rows of a column? Why didn’t you simply implement a default Sum math function in Axure capable of adding up items in a column?

Because it’s on the OnItemLoad event, it occurs once for each row.

The SUM variable starts at 0.

So on the first row, SUM = SUM + Item.price is SUM = 0 + 1.99
On the second row SUM = SUM + Item.price is SUM = 1.99 + 2.99
And so on. This happens for all the rows and once the repeater is done loading and the OnItemLoad event has fired for each row, SUM now contains the total of the Price column for each row. There is no single formula that adds up all the rows. Think of it as each row adding its value to the variable, one at a time. Like a collection plate going around a room.