How to total a repeater page.

I have a form and connected to a database collection. Form the database collection I make and connect to a dynamic page with repeater for my members to do RSVP. In the repeater, it has a column for number of attendance. Everything is going well except, I do not know how to obtain a total attendance. I will be very grateful if anyone can show me how to do this in code. FYI, I am a beginner on coding. Please see attachment. Thank you

You can try to use JavaScript reduce() method:

let initialValue = 0;
let sum = $w("#repeater1").data.reduce(
    (accumulator, currentValue) => accumulator + currentValue.attendance
    ,initialValue
);

See here: Array.prototype.reduce() - JavaScript | MDN

P.S, of course, wrap it in $w.onReady() and dataset.onReady()

I am not sure how to put the JavaScript in my editor? Can I just copy and paste onto my page code?

Yes. but put it inside:

$w.onReady( function() {

})

Also if you want it to appear in the text box, you’l need to add a line (after the code from my first post):

$w("#text1").text = sum.toString()//instead of text1, use your textbox property id

I got 2 error messages stated
[1] ‘data’ does not exist on ‘#table1’.
[2] ‘text’ does not exist on ‘#TotalAttendance

#table1 is my repeater
#TotalAttendance is the total I want it in there.

Here is my code:

$w.onReady( function () {
let initialValue = 0;
let sum = $w(“#table1”).data.reduce(
(accumulator, currentValue) => accumulator + currentValue.attendance
,initialValue
);
$w(“#TotalAttendance”).text = sum.toString()//instead of text1, use your textbox property id
})

Are you sure you used a repeater and text-box?
Could it be that you actually used a table instead of a repeater and some other element instead of a text box?

You’re right about the elements which were wrongly used. I corrected it and the errors have disappeared. But my result of my #TotalAttendance is “NaN”.??. Here is my code shown below:-

$w.onReady( function () {
let initialValue = 0;
let sum = $w(“#repeater1”).data.reduce(
(accumulator, currentValue) => accumulator + currentValue.attendance
,initialValue
);
$w(“#TotalAttendance”).text = sum.toString()//instead of text1, use your textbox property id
})

This code is based on the assumption that the field key (not field name) in your database collection is “attendance”, if the field key is different, you should change the currentValue.attendance in accordance.

I did check field key and it’s value was “attendance” in both sandbox / Live database collection. Please see attachment.

Either change the field type from Text to Number (in your database) or change the code like this:
=> accumulator, currentValue) => accumulator + Number(currentValue.attendance)

I make change to attendance’s Field Type to Number and it didn’t work, result is still NaN

Then replace your code
=> accumulator, currentValue) => accumulator + Number(currentValue.attendance)
(it give me an error on the first character(=>) Parsing error: Unexpected token )

I can think of 2 problems:

  1. Maybe you forgot to sync after you changed the field type.
  2. Maybe you didn’t wrap the code in dataset.onReady() :
 $w.onReady( function() { 
$w("#dataset1").onReady( () => {//use your dataset property id
  //THE CODE SHOULD BE HERE
} );
})

Dear JD,

Hurray! it works!
All I did was just added this line and replace (“#dataset”) as you’ve instructed;-
Thank you so much for your great help.

You’re welcome. :slight_smile:

@jonatandor35 Hey JD, Can you help me with this? I have a dataset1 and repeater1 and text1. but but can get this to work