Page 1 of 3

Calculate Sum of List Elemets

Posted: Sat Oct 04, 2014 10:31 am
by Robert Vogel

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)


Calculate Sum of List Elemets

Posted: Sat Oct 04, 2014 3:05 pm
by Alena Prykhodko

Hello,

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


Calculate Sum of List Elemets

Posted: Sat Oct 04, 2014 4:41 pm
by Robert Vogel

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


Calculate Sum of List Elemets

Posted: Mon Oct 06, 2014 3:12 am
by Yurii Orishchuk

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.


Calculate Sum of List Elemets

Posted: Mon Oct 06, 2014 6:25 am
by Robert Vogel

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


Calculate Sum of List Elemets

Posted: Mon Oct 06, 2014 11:10 pm
by Yurii Orishchuk

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.


Calculate Sum of List Elemets

Posted: Tue Oct 07, 2014 9:47 pm
by Robert Vogel

got it, thanks a lot.


Calculate Sum of List Elemets

Posted: Mon Oct 27, 2014 10:20 pm
by sebastian

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?


Calculate Sum of List Elemets

Posted: Tue Oct 28, 2014 2:29 am
by sebastian

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


Calculate Sum of List Elemets

Posted: Tue Oct 28, 2014 2:52 am
by sebastian

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