Page 2 of 3
Calculate Sum of List Elemets
Posted: Tue Oct 28, 2014 4:52 am
by Yurii Orishchuk
Hi Sebastian,
Please use following code to calculate sum:
pre
//Where "itemName" is your item name.
var items = jQuery('[name="itemName"]');
var sum = 0;
for(var i = 0; i < items.length; i++){
var item = jQuery(items);
Code: Select all
//Where "labelWithCountName" is label name that's has count.
var labelWithCount = item.find('[name="labelWithCountName"]');
var itemCount = parseInt(labelWithCount.text());
itemCount = itemCount ? itemCount : 0;
console.log("itemCount[" + i + "] = " + itemCount);
sum += itemCount;
};
alert("sum = " + sum);
/pre
Regards
Calculate Sum of List Elemets
Posted: Wed Oct 29, 2014 10:17 pm
by sebastian
The problem I'm having is that the label is in a list. I tried your script and it didn't work. I messed around with it for a few hours. The values still won't show in my label. I think the problem is the for loop is only saving the last value into the local storage.
Calculate Sum of List Elemets
Posted: Wed Oct 29, 2014 10:33 pm
by sebastian
this is actually the closest I got to getting a value placed in the correct label however it give me NaN for every single label it populates.
Calculate Sum of List Elemets
Posted: Wed Oct 29, 2014 11:05 pm
by sebastian
Even when I'm able to get values placed in the console they still give me the wrong value. I connected the script to a button hoping that I could populated all the label with a value after the list loaded after checking for the issue of the script running before the values are plugged in. Unfortunately that didn't work it only populated the first label with the wrong value.
Calculate Sum of List Elemets
Posted: Wed Oct 29, 2014 11:17 pm
by sebastian
Okay I got to display the correct values when I press my button however it only populates one label in the list still.
Calculate Sum of List Elemets
Posted: Thu Oct 30, 2014 4:16 am
by Yurii Orishchuk
Hi Sebastian,
i've provide you tested solution.
So it should work.. If id does not you need check following things(described in comments to this code).
For example:
Did you change with your names following?:
pre
1 itemName
2 labelWithCountName
/pre
Regards.
Calculate Sum of List Elemets
Posted: Thu Oct 30, 2014 10:40 pm
by sebastian
So this is my mobile layout. billAmount comes from my database collection as Q for quantity and is linked to billAmount. PN is the price and is linked to the mobilelabel_34. Both Q and PN are numbers in my collection. I want to multiply Q and PN and paste the value into mobilelabel_35.
Calculate Sum of List Elemets
Posted: Fri Oct 31, 2014 2:37 am
by Yurii Orishchuk
Hi Sebastian,
Please follow these steps:
1 Open "Success" service mapping.
2 Find link from "$" to "table" component.
3 Click "JS" on this link.
4 Add following JS code:
pre
//You need to check and correct response paraemters "Q" and "PN".
var q = value.Q;
var pn = value.PN;
var result = parseInt(q) * parseInt(pn);
element.find('[name="mobilelabel_35"]').text(result);
return value;
/pre
Regards.
Calculate Sum of List Elemets
Posted: Fri Oct 31, 2014 11:40 pm
by sebastian
Calculate Sum of List Elemets
Posted: Sun Nov 02, 2014 12:02 am
by sebastian
For values are whole numbers such as 1.00, 2.00, 3.00 and etc are not showing the decimal even when I managed to successfully use it on some of the sums with
.toFixed(2);
Its only with the whole numbers that I'm having trouble showing two zeros.
Do you have any ideas on how to fix this problem?