Small math calculations with specific repeaters data

Hi everyone,

I’m referring to the following post that has unfortunately been closed: https://www.wix.com/corvid/forum/community-discussion/small-math-calculations-with-specific-data/p-1/dl-5dfd05bcba234d0017406a12

On my website I have a sort of “wishlist” page that is linked to your profile. When you to to this page you see all the products that are in your “wishlist”. Every product has 4 different prices which I show in a textbox. At the bottom of the page I would like to have the sum of all products with the 4 different prices.

The feedback I got on the other post was helpful but the first problem is that the data I need to count up is from a repeater and therefore all the same textfield. The second problem is that the following line of code doesn’t work if the data in the textfield is variable:

$w(‘#TotalPrice’).text = String(Number($w(‘#FinalPrice1’).text) + Number($w(‘#FinalPrice2’).text));

Thanks.

Assuming your repeater is connected to a dataset you will need something like the below code to add up all the values in a repeater and show it on a text element outside the repeater.

export function dataset1_ready() {
    let sumTotal = 0;
    let sumTotalTwo = 0;
    $w("#repeater1").forEachItem( ($item, itemData, index) => {
        sumTotal = sumTotal + Number($item("#amount1").text);
        sumTotalTwo = sumTotalTwo + Number($item("#amount2").text);
    });
    $w("#final1").text = '' + sumTotal;
    $w("#final2").text = '' + sumTotalTwo;
}

Thank you very much for this!

When I try this I get the value “NaN” into my final result, have you any idea what the problem is here?

@pierreboutaine you probably have items with no amount data. It tries to include the undefined amount and it fails (as expected). You should add a condition to sum up only if a value is a number.

Here below you can see the code and the result. 2 of the 4 results are NaN and the other 2 are 19.98. This never changes, no matter how much items I have in my wishlist…

The reason why is because the code looks to take the values that are inside the text box in my editor and not the values that are in the live website which are variables… So the question is how I can link this code to the actual variables inside the text boxes…

I also see that this code doesn’t adapt itself to the amount of items I have in my live website but is always looking to the 2 items in my editor.

The pictures below show the :

  • Current code
  • Live website results
  • Editors view (here you can see the values that are in the text)

@pierreboutaine if the prices are from the database and it stored there as field type: number, then instead of:
sumTotal = sumTotal + Number($item(“#amount1”).text);

Do:

sumTotal = sumTotal + itemData.amount1;//use your collection field key

Do it for all 4 fields,
Then:

$w("#final1").text = sumTotal.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
//etc...

@jonatandor35 Thank you very much for this information.

I implemented this change and still have some problems but I think it’s almost correct. I still get the value NaN back but now for all 4 values.

FYI: On this page I have 2 databases, the first one is Producten1 (which contains the data of the products) and the second one is Boodschappenlijst (which contains the UserId and title of the products who are saved in to the wishlist, this database is connected to the database Producten1).

@pierreboutaine It’s not clear which of the, is connected to the repeater.
I’ll assume the repeater data contains the 4 prices. and that the field type is number .
Then you can do:

$w.onReady(() => {
$w("dataset1").onReady(() => {
    let data = $w("#repeater1").data;
    let sumTotal1 = data.reduce((a,c) => a + c.amount1, 0);//use you field key
    let sumTotal2 = data.reduce((a,c) => a + c.amount2, 0);
    //etc...
    $w("#final1").text = sumTotal1.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
//etc...
})
})

Thanks again!!!

Hhhmmm but there still seems to be something wrong… I again get the result NaN…

Here below you can find the code, I just implemented it for one of the 4 to see what it would gave.

  • wishlist = my repeater
  • prijsCarrefour = the field in my Producten1 database that contains the price for each item (I have 4 similar fields like this in my database but I now just did the test for this on)
  • TotalPriceCarrefour = The text field where the end and total price of the calculation should be

I also have the second database called Boodschappenlijst, this is the database that is connected (reference) to the database Producten1. The database called Boodschappenlijst contains who has wish items in his wishlist.

What could it be?

I can’t see your code.

  • please confirm that all the 4 fields are of type Number and that they all contain numeric values.

@jonatandor35 In the images below you can find the:

  • Code
  • Database Boodschappenlijst (that is connected to database Producten1)
  • Database Producten1

The fields are indeed of field typer Number as you can see in the image.

Your help is much appreciated.

@pierreboutaine
and is the Producten1 connected to the repeater?

  • I can’t see if all the records have values.
    Anyway, if you’re missing some values, you can modify the code like this:
$w.onReady(() => {
$w("dataset1").onReady(() => {
    let data = $w("#repeater1").data;
    data.forEach(e => {
        if(!e.amount1){e.amount1 = 0;}
        if(!e.amount2){e.amount2 = 0;}
        //etc..
    });
    let sumTotal1 = data.reduce((a,c) => a + c.amount1, 0);//use you field key
    let sumTotal2 = data.reduce((a,c) => a + c.amount2, 0);
    //etc...
    $w("#final1").text = sumTotal1.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
//etc...
})
})

@jonatandor35 Thank you for this.

I just implemented this and now strange enough I get the result 0,00.
It seems like something is wrong in the code and therefore he thinks all values are empty and therefore replaces them with 0…

@pierreboutaine under the “let data” before the forEach method, add a line:

console.log(data);

And look at the console log. Check the price values and locate the problem.