Fields number calculation

Here i am asking about tournament leaderbord module.

I will make Admin section later on where we will do inputs for each person and data will be updated. I will have inputs: “points1 + points2 +points3 = totalPoints”. How do i make mathematical calculation for inputs to get totalPoints so we dont need to calculate manually what are totalPoints.

Thanks!
@ahmadnasriya

Ok so i made some progess in it. Action is happening in repeaters row where displayed different person with different point1 points2 field results in dataset1

export function repeaterTeam_itemReady($item, itemData, index) {
 
 // no need to set the filter on the database since it's still set

 // how many items filtered in the dataset?
 let count = $w("#dataset1").getTotalCount();
 
 // get all of the items
   $w("#dataset1").getItems(0, count)
   .then((results) => {
 let sumTotal = 0; // declare sum
 let items = results.items;
    items.forEach(item => {
 // the amount is a string so convert
       sumTotal = sumTotal + Number(item.points1) + Number(item.points2);
    });
    $w("#textSum").text = "" + sumTotal;
    }).catch((err) => {
       console.log(err);
    });
}

Problem is it get total sum for every users points1 and points2 values. How do i separate that so #textSum displays values from RepeaterMemberOnly points1 points2 values? I hope i explained it clearly hehe.

Thanks
@Ahmad @anthonyb @Yisrael (Wix)

I read the post multiple times trying to figure out what exactly do you mean, but I couldn’t.

What are you trying to achieve?

Ok sorry for unclear explanation:
Here is sample:

Database for each User:

Repeaterf for each User and TotalPoints text field where sum of points1, points2 etc. has to be displayed.

Do you want to sum all the points of a given user and display them on a repeater? If that’s the case, follow with this:

$w('#repeater').onItemReady(($item, data) => {
    let points = 0;
    for (let i = 1; i <= 5; i++) {
        let value = data[`points${i}`];
        points = points + Number(value);
    }
    
    $item('#totalPoints').text = String(points);
})

Hope this helps~!
Ahmad

Gets the data but does not sumup. Last field is empty there fore its undefined


@ahmadnasriya

@ahmadnasriya this one
At the same time, could you give me another sample for the same code but retrieving data from dataset instead, not from repeater, and displaying it in a text field? I tried a bit but coul not figure out.

Answer updated.

For sure:

let item = $w(dataset).getCurrentItem();
$w('#score').text = String(item.score);

@ahmadnasriya Spot on! Both are working like they should!
The very last thing i would appreciate for this. case is:

This code is used for personal points:

$w('#repeater').onItemReady(($item, data) => {
    let points = 0;
    for (let i = 1; i <= 5; i++) {
        let value = data[`points${i}`];
        points = points + Number(value);
    }
    
    $item('#totalPoints').text = String(points);
})

I would like to do the same but use it for Team points.
Team is based from 2 persons and totalTeamPoints are based on upper values for each person. I need to sum up those 2 values and display result.
I have main personal dataset + personal points dataset, teams dataset + team points dataset.
Now all of the personal points, teams, team points datasets has reference field with main personal dataset. So whatever changes are made on all 3 dataset it updates values for mainPersonal dataset. Maybe it sounds complicated here but my goal is to avoid doing manual inputs for totalTeamPoints.

In the repeater for teamScores i am displaying teeam mebers from mainPersonalDataset. So i have linked teamCaptain and teamAssistant fields.
Each person i linked whether he is captain or assitant. I assume we could use these 2 fields to combine captain and assistant points to get totalTeamPoints?
That way we can pull out correct _id from captain and assistant and get correct numbers in repeater.

I could of course enter totalTeamPoints manually and just connect with proper field so information is displayed, thats easy. But goal is to make total autonomy here.

Is that complicated?