Page 1 of 1

Sum of Variables in a List of Same Component

Posted: Tue Dec 23, 2014 7:21 am
by Matthew6439702

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


Sum of Variables in a List of Same Component

Posted: Wed Dec 24, 2014 2:15 am
by Yurii Orishchuk

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.


Sum of Variables in a List of Same Component

Posted: Tue Jul 21, 2015 2:30 am
by Jack Bua

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


Sum of Variables in a List of Same Component

Posted: Wed Jul 29, 2015 12:36 pm
by Yurii Orishchuk

Hi Jack,

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

Regards.


Sum of Variables in a List of Same Component

Posted: Wed Jan 17, 2018 6:18 am
by Deon

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


Sum of Variables in a List of Same Component

Posted: Wed Jan 17, 2018 4:11 pm
by Serhii Kulibaba

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


Sum of Variables in a List of Same Component

Posted: Thu Jan 18, 2018 3:05 am
by Deon

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


Sum of Variables in a List of Same Component

Posted: Thu Jan 18, 2018 6:57 pm
by Serhii Kulibaba

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