Robert Vogel
Posts: 0
Joined: Sat Oct 04, 2014 10:31 am

Calculate Sum of List Elemets

I have a List Element full of minutes and want to compute the sum of all these elements via JS. How do I need to access the different list Element, so that I can use:

Array.prototype.sum = function () {
var w = Appery('mobile_list');
var total = 0;
var i = w.length;

Code: Select all

 while (i--) { 
     total += w[i]; 
 } 

 return total; 

}

(does not work)

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Calculate Sum of List Elemets

Hello,

Do you populate list from database?
Then this topic will help https://getsatisfaction.com/apperyio/...

Robert Vogel
Posts: 0
Joined: Sat Oct 04, 2014 10:31 am

Calculate Sum of List Elemets

Hello Alena, hello Team,

thanks for your feedback, it gave me some hints but does not solve my challenge.
Here is the DB I defined:

Image

and the StartScreen.

Image

I defined the List DB Service and a local storage variable.
Here is the mapping and the JS Code for the local storage variable

Image

Image

I think this is wrong :-(

Finally I want to assign a label out of the local storage variable on a click event.

Image

It seems, that it is only a small thing, but after reading all your manuals and testing around for hours I am not able to find it.

So hopefully you can help.

Thanks a lot
Robert

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

Calculate Sum of List Elemets

Hi Robert,

Currently it's not clear what you want to calculate.

But here is an easy way to do it:

1 Add "success" event handler with JS action type.

2 Populate it with following code:

pre

var count = 0;

for(var i = 0; i < data&#46;length; i++){
count += data&#46;Minuten;
};

&#47;&#47;Store count for further use when need&#46;
localStorage&#46;setItem("count", count);

alert("count = " + count);

/pre

If it does not help - please specify final goal that you want to reach.

Regards.

Robert Vogel
Posts: 0
Joined: Sat Oct 04, 2014 10:31 am

Calculate Sum of List Elemets

Hi Yuril and Team,

thanks a lot for your response.

I map the DBVar (in my example I name it Minuten) to a local storage variable (here in my example I name it SummeMinuten)
I know the local Storage variable is a string and I get from the database an array of strings. So in the JS of the mapping I need to set the local variable in a specific way.

That code is what I need.

Thanks a lot
Robert

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

Calculate Sum of List Elemets

Hi Robert,

In your mapping you just link "first item"-Minuten to "SummeMinuten" lsv.

So you can use this LSV in context of first item-Minuten.

If you want it you can get this LSV in following way:

pre

var summeMinuten = localStorage&#46;getItem("SummeMinuten");

&#47;&#47;Using value&#46;
alert("summeMinuten = " + summeMinuten);

/pre

But if you need use it in other way please specify it.

Regards.

Robert Vogel
Posts: 0
Joined: Sat Oct 04, 2014 10:31 am

Calculate Sum of List Elemets

got it, thanks a lot.

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

Calculate Sum of List Elemets

Image

I'm working on a similar project where I need users orders to be calculated from a list populated by a database. How can I sum up the values of all the objects in the list?

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

Calculate Sum of List Elemets

for each PN value how do I multiple that for each Q value? Image

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

Calculate Sum of List Elemets

Okay so I figure that out but I'm having issues mapping out the individual values in the label per item. I map it out correctly from storage but its not plugging in the value for each item. Image

Return to “Issues”