2

I have a filtered list as shown on the screenshot below

screenshot of the filtered list

I would like to calculate to total of "Km" in a separate field.

Can you help me ?

Here is the screen shot with the structure of the widget :

Structure Admin View

Michaël_B
  • 25
  • 3

1 Answers1

1

Something like the following will work:

TableListWidget.children._values
  .map(function(obj){ 
    return parseInt(obj.descendants.nameOfYourKMField.value);
  })
  .reduce(function(accumulator, currentValue){ 
    return accumulator + currentValue; 
  });

TableListWidget.children._vaues contains an array of all your table rows, the descendants property of each row will contain the widget which has your KM value. We map the _values array to an array of KM field values and then use the reduce function to add them.

Rherma
  • 331
  • 2
  • 8
  • Thanks, I don't know what to replace "TableListWidget". I have tried Table1Panel Table1 Table1Body Table1Row Without success. – Michaël_B Aug 17 '18 at 07:51
  • You need to replace `TableListWidget` with whatever the table's list widget is. You'll also need to replace `nameOfYourKMField`. So if you want some label to display the total number of KM you'll need to switch that label's onDataLoad event and have `TableListWidget` be `widget.root.descendants.Table1Body`. – Rherma Aug 17 '18 at 14:12
  • Just noticed you attached the structure of your page. In this case if you wanted to have some label display the total you would use the following for your onDataLoad event: `widget.text = widget.root.descendants.Table1Body.children._values.map(function(obj){ return parseInt(obj.descendants.Field1.value); }).reduce(function(accumulator, currentValue){ return accumulator + currentValue; }).toString();` – Rherma Aug 17 '18 at 14:22
  • Just did it. Sorry, I did'nt know how to do it before. :-) Thank you again – Michaël_B Aug 23 '18 at 10:19
  • does this also cater decimal value eg: 10.99 – alep Oct 04 '19 at 10:04