Calculate sum in a repeater


#1

Hi,

I have a repeater, where inside each row I have a textfield where the user can input a numeric value.

I want to be able to calculate the sum of this somehow, but as it is a repeater I can’t just add the values together. Is there a way for me to calculate the sum? (There will be different values on different rows, and different amount of rows depending on user’s actions).

Many thanks!


How can I multiply number in repeater, clicking on plus
#2

Here’s one way to do it - on change of the text field, you update the repeater and then have an onItemLoad event that adds the value to the total.

Three things to note:

  1. Remember that anytime the repeater is updated (filtered, rows added, removed, etc) the data will refresh. So any data or changes you want to persist need to be updated in your data set.

  2. The “isFirst” action is important as it resets the total each time. Without this your numbers will just keep growing.

  3. Because i’m using “onLostFocus” to update the row, tabbing won’t work here. It’s a bit of a trade-off to make this work. There are other ways but they require a lot more work (read javascript hackery).

So here’s a super quick and dirty sample of the approach:

RepeaterSum.rp (57.2 KB)


#3

Ok, thank you for the reply!

I’ve been trying hard to find a way to solve it without refreshing it as there are so many other objects in each row that might been changed.


#4

Do you have the need to sort/filter/add or remove rows? If not, I can show you a different way without updating the repeater.


#5

No need to sort or filter, but preferably add/remove.

Many thanks,


#6

Here’s the approach without updating the repeater. Instead of updating the row and triggering the logic, we put an onMove event onlost focus. I move the total value by 0,0 (no visual effect, but it allows us to trigger an event).

onMove of the total, I reset the total to 0 and then move the text input that’s inside of the repeater. This takes advantage of the fact that if you trigger an action on an element inside a repeater from outside of the repeater, it fires on EVERY instance of that element.

As noted before though - if you add or remove rows, any data changes in the repeater will be reset unless you persist those changes in the data set and apply them onItemLoad.

RepeaterSum_NoUpdate.rp (57.3 KB)


#7

Thank you!!!

I had to tweak it a bit to fit my solution but ultimately solved it using this logic!

Yea, still updates when adding or removing rows but I think it’s fine, most important was to not having it update while changing the values. So thank you!


closed #8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.