Calculate Total Hours in Grid - Result into TextBox

ProcessMaker code contributions

Moderator: amosbatto

HSchlierkamp
Posts: 8
Joined: Thu Sep 25, 2014 4:31 am

Calculate Total Hours in Grid - Result into TextBox

Unread postby HSchlierkamp » Thu Nov 06, 2014 4:52 am

Good Day,

Hope Someone can Help me with a Dilemma of Time Calculations Total Hours and minutes and Place result into a Text Box outside of the Grid,
I have attached my Process to make it easy to have a look, my Java code is in the process.

Thank you kindly for that advice and Help


Kind Regards,
Attachments
IT_008_Overtime_or_Stand_By_Sheet.pm
Select Overtime to get the grid
(105.38 KiB) Downloaded 75 times
PM Version 2.5.2.5 with Hotfix
PM Version 2.8 - Testing
Ubuntu Servers

pfsilva
Posts: 62
Joined: Mon Nov 24, 2014 4:35 pm

Re: Calculate Total Hours in Grid - Result into TextBox

Unread postby pfsilva » Wed Nov 26, 2014 5:09 pm

Hi guys, if you need to get the sum or average of a grid column, you can use Aggregate functions, to get more information about them click on the following link:
http://wiki.processmaker.com/index.php/ ... _functions

You can also use javascript to loop through the rows in the grid and get the value of the field you want to work with.

A quick sample

Code: Select all

function sumGridColumn(gridName, fieldName) {
    /* Get the number of rows in the grid */
    var rowCount = Number_Rows_Grid(gridName, fieldName);
    var total = 0;
    for (var i = 1;i <= rowCount;i++) {
        /* Get the row's field value */
        total += parseFloat(getGridField(gridName, i, fieldName).value);
    }
    return total;
}

/* Set the value of the field that'll hold the sum */
getField('targetTotalField').value = sumGridColumn('yourGridName', 'fieldToSum');

I chose to use a function in my javascript code because I might need to calculate other columns on demand, this is just a basic example, you can modify the code to do any necessary validation.

@HSchlierkamp
Your case is a bit different since you want to sum hours, I've checked the function you're using and found that some variables don't match, even after fixing that issue you won't get the results you want because of time calculation, I would recommend you to work with minutes, so you'd need to create two functions, one to convert your hh:mm formatted time to minutes, and one to convert minutes to hh:mm formatted time, once you have these functions, you can loop through the grid rows, convert each field value to minutes, sum the total of minutes and at the end convert them to hh:mm formatted time to use that value in the Total Hours field.

.
Pedro F. Silva
Senior Customer Support Engineer
ProcessMaker Inc.

HSchlierkamp
Posts: 8
Joined: Thu Sep 25, 2014 4:31 am

Re: Calculate Total Hours in Grid - Result into TextBox

Unread postby HSchlierkamp » Wed Feb 11, 2015 3:11 am

Good Day,

Pardon me for only coming back now, have a lot on my plate.

@fareed:
Not sure what you mean with "Can you Tell me how you populate the System variable in the form"
I use triggers a lot which I create, to populate fields, Please let me know what exactly you would need or like to understand and I will help with that gladly.

@pfsilva:
Thank you kindly for your Example code. Starting to get my head around javascript, was hoping that the time calculation will be bit easy, My fault in thinking that.
I do understand what you mean with the conversion of time to minutes and back again, Think i will do this with a hidden fields. before posting the result back to user.

Thank you kindly for the reply and assistance.
Once I have this working I will post back the PM process.

Kind Regards,
HSchlierkamp
PM Version 2.5.2.5 with Hotfix
PM Version 2.8 - Testing
Ubuntu Servers


Return to “Code”

Who is online

Users browsing this forum: No registered users and 1 guest