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:
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.
@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
@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...
})
})
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.
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…