Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Calculate Sum of List Elemets

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&#46;length; i++){
var item = jQuery(items);

Code: Select all

 &#47;&#47;Where "labelWithCountName" is label name that's has count&#46; 
 var labelWithCount = item&#46;find('[name="labelWithCountName"]'); 

 var itemCount = parseInt(labelWithCount&#46;text()); 

 itemCount = itemCount ? itemCount : 0; 

 console&#46;log("itemCount[" + i + "] = " + itemCount); 

 sum += itemCount; 

};

alert("sum = " + sum);

/pre

Regards

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

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.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

Image

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.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

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.

Image Image

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

Okay I got to display the correct values when I press my button however it only populates one label in the list still.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Calculate Sum of List Elemets

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.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

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.

Image

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Calculate Sum of List Elemets

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

&#47;&#47;You need to check and correct response paraemters "Q" and "PN"&#46;
var q = value&#46;Q;
var pn = value&#46;PN;

var result = parseInt(q) * parseInt(pn);

element&#46;find('[name="mobilelabel_35"]')&#46;text(result);

return value;

/pre

Regards.

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

It worked! Thanks Yurri!

sebastian
Posts: 1
Joined: Tue Oct 21, 2014 9:46 pm

Calculate Sum of List Elemets

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?

Return to “Issues”