### Calculate grid field for Total

Moderator: amosbatto

### Calculate grid field for Total#783970

By walshie1973
#783970
We are running Processmaker 3.0.0.5 and I have been trying to do a calculation of a grid field called Total (see below)
RecruitmentTests01.png (20.39 KiB) Viewed 2055 times
If the "Required" field is checked, then the total should equal the cost field. If it is unchecked, then the amount should be 0.00. I would then like to use the sum of the "Total" field in other calculations on the Dynaform.

I have tried so many different ways of coding this, which haven't worked, so it would be pointless to even show any of it here.

Any suggestions how to do this in version 3.0?

### Re: Calculate grid field for Total#786657

#786657
Hi walsh 1973

Let you know that there is an option in Grids for the same Dynaform, you of the total according to the fields that you enter it, you attached a picture, hope I helped.

Greetings.

Best Regards,

Software Quality Engineer
ProcessMaker
##### Attachments
suma.jpg (72.63 KiB) Viewed 172 times

### Re: Calculate grid field for Total#814231

By cpomares
#814231
Hi walshie1973,
As far as i understand, you want something similar to this:
Selection_004.png (20.23 KiB) Viewed 1505 times
You can do it using JavaScript, here i show you the code i used:
Code: Select all
``````var sum =0;
for (var i=1;i<=\$("#gridVar002").getNumberRows();i++){
if(\$("#gridVar002").getValue(i,2) == "1"){
\$("#gridVar002").setValue((parseFloat(\$("#gridVar002").getValue(i,3))),i,4);
}
else{
\$("#gridVar002").setValue(0,i,4);
}
sum=sum+\$("#gridVar002").getValue(i,4);
}

\$("#textVar002").setValue(parseFloat(sum));``````
Where "gridVar002" is the ID of the grid and "textVar002" is the ID of the final text box where i display the sum of the total.

Regards,
Carlos

### Re: Calculate grid field for Total#815645

By PipSqueak
#815645
Hi, I added setonchange, but this did not work.
Code: Select all
``````\$("#form").setOnchange(function(field, newVal, oldVal) {
var sum =0;
for (var i=1;i<=\$("#gridQuotes").getNumberRows();i++){
if(\$("#gridQuotes").getValue == "1"){
\$("#gridQuotes").setValue((parseFloat(\$("#gridQuotes").getValue(i,2))),i,5);

}
else{
\$("#gridQuotes").setValue(0,i,5);
}
sum=sum+\$("#gridQuotes").getValue(i,5);
}

\$("#textVar002").setValue(parseFloat(sum));
});``````

### Re: Calculate grid field for Total#815663

By amosbatto
#815663
PipSqueak,
You need to use the ID of the Dynaform, like this:
Code: Select all
``````var formId = \$("form").prop("id");
\$("#"+formId).setOnchange(function(field, newVal, oldVal) {
var sum = 0;
for (var i = 1; i <= \$("#gridQuotes").getNumberRows(); i++) {
if (\$("#gridQuotes").getValue == "1") {
\$("#gridQuotes").setValue( (parseFloat(\$("#gridQuotes").getValue(i,2))), i, 5);
}
else{
\$("#gridQuotes").setValue(0,i,5);
}
sum = sum + \$("#gridQuotes").getValue(i,5);
}

\$("#textVar002").setValue( parseFloat(sum) );
});
``````

### Re: Calculate grid field for Total#815669

By PipSqueak
#815669
I did that but everything just turns 0 on the grid.
2018-08-14 12_22_09-(wei-ni.chong in test).png (15.09 KiB) Viewed 1214 times
I've included an export of the dynaform. Basically, I'm trying to get the top choice quote and compare it to the itemised costs to make both of it match before the user can submit it.

### Re: Calculate grid field for Total#815705

By PipSqueak
#815705
There has got to be an easier way than this?

### Re: Calculate grid field for Total#815707

By amosbatto
#815707
Pipsqueak,
What are trying to do?
I can't figure out it from your code since it looks like you are trying to set values inside the grid, but you are also trying to check values at the same time.

### Re: Calculate grid field for Total#815710

By PipSqueak
#815710
Basically, when the user checks "top choice" on the quote, I want it to go onto the Total Column and the non-top choice quotes will be \$0. Then I want to compare the sum of the Total Column to match with the Itemised Cost Grid because they have to match.

### Re: Calculate grid field for Total#815734

By amosbatto
#815734
You want all the rows in the grid which have "Top Choice" marked to be summed?
Then you want that total to be equal to the sum of the "Cost" column in the "Itemized Costs" grid. Right?

### Re: Calculate grid field for Total#815744

By PipSqueak
#815744
amosbatto wrote: Fri Aug 17, 2018 1:23 am You want all the rows in the grid which have "Top Choice" marked to be summed?
Then you want that total to be equal to the sum of the "Cost" column in the "Itemized Costs" grid. Right?
Yes, that's correct. The user will choose only one "Top Choice" and it will appear in the "Total" column. Then in the "itemised cost" grid, they'll break down the costs of the (top choice) quote and the sum of the itemised costs will need to match the top choice quote otherwise the form won't get submitted.

Thanks

### Re: Calculate grid field for Total#815768

By amosbatto
#815768
I think this is what you want:
Code: Select all
``````\$("#"+formId).setOnSubmit( function() {
//search for the row which is marked as the "Top Choice":
var total = null;
var nRows = \$("#gridQuotes").getNumberRows();
var costColNo = 2; //set to the column number of the 'Cost' field in the grid
var totalColNo = 5 //set to the column number of the 'Chosen' field
var topChoiceColNo = 4; //set to the column number of the "Top Choice" checkbox

for (var i = 1; i <= nRows; i++) {
var topChoice = \$("#gridQuotes").getValue(i, topChoiceColNo);
if (topChoice == "1") {
total = parseFloat( \$("#gridQuotes").getValue(i, costColNo) );
\$("#gridQuotes").setValue(total, i, totalColNo); //copy to "Chosen" field in the same row
break;
}
}

if (total === null) {
alert("Need to select an item as the 'Top Choice' in the Quotes list.");
return false;
}
else if (isNaN(total)) {
alert("Need to enter a valid number in the 'Top Choice' row in the Quotes list.");
return false;
}

var itemisedTotal = parseFloat( \$("#gridItemised").getSummary("strgItemisedCost") );

if (total != itemisedTotal) {
alert("The cost of the Top Choice in the Quotes list must match the summed total of the Itemised grid.");
return false;
}
});``````
###### External links in an output document

Hi everyone! I am trying to create a link to a ex[…]