Skip to main content
Skip table of contents

Looping Through Datagrid Items (GlobalForms versions < 10.1)

Version Compatability

Prior to GlobalForms version 10.1, Currency Components stored the underlying data entered as a string instead of a number. In GlobalForms 10.1, this was changed to both simplify rule logic and to be more consistent with what would be expected by most rule authors. This version of the documentation accounts for the data being stored as a string, which may be useful for this and other scenarios where treating string data as a number in a rule is important.


This example is just a model for how to loop through each item in the Data Grid, when the data itself is stored as a string (GlobalForms versions less than 10.1).  You may need to modify this example to fit your specific use case and handle error conditions.  This example assumes a Data Grid component with the API Property Name of expenseLines and a Currency Component with an API Property Name of expenseAmount in the grid.

CODE
//Initialize a variable to store the calculated amount.
var amount=0;

//Basic check to make sure there is data to calculate.
if(data.expenseLines[0].expenseAmount)
{
	//Loop through each line.
	data.expenseLines.forEach(function(item) { 
    	if(item.expenseAmount){
			//Add the line's data to the total be tracked.  Using parseFloat to make sure we are adding numbers and not concatenating strings.
        	amount = amount + parseFloat(item.expenseAmount.replace(',','')); 
    	}
	});
}

//Assign the calculation to the field.
value = parseFloat(amount).toFixed(2); 
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.