Hi,
As part of a much longer script, I need to loop through each line of the Grid and add the value in the 'Adjusted hours' field on each row to the 'Adjusted hours' value on the next row etc until I get the total. I've got as far as getting it to add the value of each line to itself (not q the plan!), but not to the value on each subsequent row.
I know on screen in the Grid that the Total is shown, but as far as I know, this is not a element that is available for use in scripting, hence needing to tell it in the script how to do this.
The code I have so far for this is:
for (var gridindex in item.grid) { var Totadjhrs = item.grid[gridindex].TS_ADJ_HRS + item.grid[gridindex].TS_ADJ_HRS; }
I can see now why this adds the value to itself; what I don't know is what I need to write to get it to take one instance of the value and add it to the next one until it has completed all rows of the Grid and come out with the overall total.
Thanks for your help!
Fiona
Solved! Go to Solution.
Solved by dvirh. Go to Solution.
Hi Fiona,
One way would be to store the previous values in an array. As you loop through the grid records, you would push the current adjusted hours value to your array, then check for gridindex being strickly greater than zero and calculate the total value there by adding current adjusted hours value to value found in previous row (i.e. calling your array with gridindex minus one).
I hope this helps.
Regards,
hi Fiona,
There are a couple of issues with your script. First, you are defining the variable inside the loop. Second, you are not adding the value to the prior sun. Try this instead:
var Totadjhrs = 0;
for (var gridindex in item.grid) { Totadjhrs = Totadjhrs + item.grid[gridindex].TS_ADJ_HRS; }
Hi Fiona,
Here is code that worked for me:
var previousGridArr = []; for (var gridindex in item.grid) { var curValue = item.grid[gridindex].ADJUSTED_HOURS; previousGridArr.push(curValue); if (gridindex > 0) { previousVal = previousGridArr[gridindex - 1]; var total = curValue + previousVal; item.grid[gridindex].ADJUSTED_HOURS = total; } }
Regards,
Thanks for your reply Bastien,
In this case, it's probably more than I need, but good to see an alternative way and to keep learning (or trying to!). No doubt it will be of help to someone else at some point too.
Fiona