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
top of page
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
bottom of page
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.
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 did check field key and it's value was "attendance" in both sandbox / Live database collection. Please see attachment.
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 }) -------------------------------------------------------------------------------------------------------
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?
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 })
---------------------------------------------------------------------------------------------
I am not sure how to put the JavaScript in my editor? Can I just copy and paste onto my page code?
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: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
P.S, of course, wrap it in $w.onReady() and dataset.onReady()