Matthew6439702
Posts: 0
Joined: Sun Nov 24, 2013 1:10 am

Sum of Variables in a List of Same Component

I want to add the returned variables of the same component in a list. I don't want to count the loops but add the values returned together into a total.

I want to add the sum of the NUMBERS on the RIGHT, "Time," that was returned by a query service, and fill the TOTAL TIME label on the left. I have been searching for hours on this forum and found nothing that really works.
Image

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

Sum of Variables in a List of Same Component

Hi Matthew,

Please follow this solution:

  1. Add "Success" event handler for the service that populates your grid.

  2. Use following code for this event handler:

    pre

    //where "time" is response collumn that has time value from where you populate "time" component.
    var timeField = "time";

    var sum = 0;
    for(var i = 0; i < data&#46;length; i++){
    var time = parseInt(data[timeField]);

    console&#46;log("time for " + i + " item is " + time);
    if(time)
    sum += time;
    };

    console&#46;log("sum = " + sum);

    &#47;&#47;Where "totalName" is total component name&#46;
    Apperyio("totalName")&#46;txt(sum);

    /pre

    Also this code has comments and debug logs please read it.

    Regards.

Jack Bua
Posts: 0
Joined: Sun Jun 28, 2015 10:16 pm

Sum of Variables in a List of Same Component

could you provide an example of what "time" should be?

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

Sum of Variables in a List of Same Component

Hi Jack,

In this case "time" is DB collection field where you store a number(like 60, 90, 180 etc..).

Regards.

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Sum of Variables in a List of Same Component

Hi
I have an identical requirement. This is not working besides the syntax error in your code.....

Apperyio("totalName").txt(sum);
Should be
Apperyio("totalName").text(sum);

Console return 0 for sum and does not return anything for time. It seems there may be an issue with
var time = parseInt(data[timeField]);
... as not further execution happens

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Sum of Variables in a List of Same Component

Deon, you are right, it has to be a method 'text'

Deon
Posts: 0
Joined: Sun Jun 30, 2013 6:00 am

Sum of Variables in a List of Same Component

Yes but that is not the problem as I said. It still does not work.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Sum of Variables in a List of Same Component

Could you clarify, what exactly doesn't work and what functionality do you need to have?

Return to “Issues”