Grid Add - Running Total

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
juvat
User
Posts: 71

Grid Add - Running Total

Post by juvat »

Is there a way to include a Running Total when in Grid Add?

Example: I have a timesheet screen were employees add their hours using the Grid Add screen of the data table. After they have entered their hours a total for the day appears under the entries, but what I'd like to be able to add is a way to show the total # of hours before the checkmark is clicked.


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

You need to use Client script like JavaScript/JQuery to do a "live calculation".

Following is demo-code that you need to change to fit your project...

Client-side Events (JavaScript)
1) Click on a field to auto-calc; on right pane choose Client-side Events
2) Past bellow content; change field names to fit your project

{ // keys = event types, values = handler functions
"change keyup": function(e) {
var $row = $(this).fields();
var st = $row["UnitPrice"].toNumber() * $row["Quantity"].toNumber();
$row["SubTotal"].value(st);
}
}

3) Make field SubTotal read-only in Edit-, Add- and List page:

Copy this:
$this->SubTotal->ReadOnly = TRUE;

Into following sections:
Client Scripts -> Table-Specific -> Add/Copy Page -> Page_load
Client Scripts -> Table-Specific -> Edit Page -> Page_load

4) Activate Client-Side (Java Script) validation


If you have a master/detail view and need to sum a row-total-column (detail) and store the total in a field in master table:

Sum column and show in field - and handle comma as decimal delimiter (Master/Detail) (JavaScript)
Master table! -> Client Script -> Table_Specific -> Add/Copy Page -> Client Script

////////////////////////////////////
// Calculate total sum of order...
////////////////////////////////////
var $form = $('#forderadd'), //ID of form to monitor
$summands = $form.find('[data-field =x_Sums]'), //Element: data-field should be x_Sums
$sumDisplay = $('#x_OrderSum'); //ID of input field to present the result

//I form id="forderadd" ...
$form.delegate('.form-control', 'change', function ()
{
var sum = 0;

//For each datafield=x_Sums
$summands.each(function ()
{
      //Replace comma decimal to dot...
    var value = $(this).val().replace(",", ".");

      //Change data type to float...
    value = parseFloat(value);

      //If valid number add to sum
    if (!isNaN(value)) sum += value;
});

 //Make two deciamals and change back decimal separator to comma
sum=sum.toFixed(2).replace(".", ",");

 //Store sum to field
$sumDisplay.val(sum);

});

You can search about this in the forum and you'll find more info.


kirondedshem
User
Posts: 642

Post by kirondedshem »

If you still dont get it, there is a simmilar answer on this topic "Total Qty placement under detail table" on http://www.hkvforums.com/viewtopic.php?f=4&t=40925, just change to your table field names.


ameisfine
User
Posts: 74

Post by ameisfine »

I try this
{ // keys = event types, values = handler functions
"change keyup": function(e) {
if (this.value == "N") {
var $row = $(this).fields();
var $one = 1000000;
var $two = 2000000;
var st = ($row["pwidth"].toNumber() * $row["pdepth"].toNumber() * $row["pheight"].toNumber()) / $one;
$row["vol_pack"].value(st);
}
else if (this.value == "Y") {
var $row = $(this).fields();
var st = ($row["pwidth"].toNumber() * $row["pdepth"].toNumber() * $row["pheight"].toNumber()) / $two;
$row["vol_pack"].value(st);
}
}
}

Result : "N" is working while "Y" is not working.
Also the field "vol_pack" is writable (usually I use this method, field is unwritable)
Whats not correct ?


kirondedshem
User
Posts: 642

Post by kirondedshem »

var st = ($row["pwidth"].toNumber() * $row["pdepth"].toNumber() * $row["pheight"].toNumber()) / $two;

YOu are using $two yet it is only defined within the first if statement so it might not be accessible in the else part, try putting it outside before any if statements


juvat
User
Posts: 71

Post by juvat »

Thanks riverman . I tried adding (modifying only the field name) your example code and activating client side validation, but didn't see any difference when I uploaded a test. Should the sub-total appear at the bottom of the grid-add or somewhere else?


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

The code:
$row["SubTotal"].value(st);

The value of st will be stored in field SubTotal. So you need to create a field for subtotal.

Ex of output.
Qty price subtotal
1 100 100
2 50 100


Post Reply