Detail Table Field value Summation

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

Detail Table Field value Summation

Post by ebinnasir »

Hello,

Hope all are fine.

In my master-detail form, i want to show summation result of detail table grid field value. Detail table field name is return_quantity and result will show on pre_approval field of master table. I placed the below code on Table Specific->Add/Copy Page->Client Script of master table:

var $form = $('#fgrayreturn_hubadd'),
    $summands = $form.find('[data-field="x_return_quantity"]'),
    $sumDisplay = $('#x_pre_approval');

$form.on('change', '.form-control', function () {
    var sum = 0;

    $summands.each(function () {
        var value = $(this).val();
        value = parseFloat(value);

        if (!isNaN(value)) {
            sum += value;
        }
    });

    sum = sum.toFixed(2);
    $sumDisplay.val(sum);
    console.log('st:', sum);
});

Using the above code first row of return_quantity field value is showing on pre_approval field of master table. But 2nd row and so on row of detail table return_quantity field value is not doing sum. Console log also showing first row value without any error. I don't find any solution of that. Can experts identify where the mistake is and why all the rows value of details table is not doing summation and showing the result on the defined master table field?

Thanks in Advance.

Regards


arbei
User
Posts: 9384

Post by arbei »

  1. You selected all '.form-control in the Master/Detail-Add form, you only select the inputs for the "return_quantity".
  2. You added your "change" listener to the form, you better add them to the inputs for the "return_quantity".
  3. You should add your code in Startup Script (not Client Script) because you need to select the inputs after the inputs are outputted.
  4. You better use Client side events instead.

ebinnasir
User
Posts: 103

Post by ebinnasir »

I tried the below codes on client side event of detail table return_quantity field.

var $row = $(this).fields(); 

var returnqty = $row["return_quantity"].value();
var quantity = parseFloat(returnqty.replace(/,/g, ""));
console.log('quantity:', quantity);

var total = 0.00;

if (isNaN(quantity) == true) {
quantity = 0;
}
total = parseFloat(total) + quantity;
//console.log("total_qty:", total);
document.getElementById('x_pre_approval').value += total;

This codeis not workin as expected. Suppose 1st row value is 10 and 2nd row value is 120. This code is showing result 10120. Can experts please solve this problem.

Regards


arbei
User
Posts: 9384

Post by arbei »

  1. Your code needs to be in your funcction (the event handler), see the example.
  2. You should get all quantities (not just the current row) and sum them up.

ebinnasir
User
Posts: 103

Post by ebinnasir »

Hello,

I am stuck on the below problem. Can any expert please give solution :

var $row = $(this).fields(); 

var returnqty = $row["return_quantity"].value();
var quantity = parseFloat(returnqty.replace(/,/g, ""));
console.log('quantity:', quantity);

var total = 0.00;

if (isNaN(quantity) == true) {
quantity = 0;
}
total = parseFloat(total) + quantity;
//console.log("total_qty:", total);
document.getElementById('x_pre_approval').value += total;

This code is not working as expected. Suppose 1st row value is 10 and 2nd row value is 120. This code is showing result 10120. Can experts please solve this problem.


arbei
User
Posts: 9384

Post by arbei »

ebinnasir wrote:

var total = 0.00; // total is reset to 0
total = parseFloat(total) + quantity; // Add quantity of current row to total => total is always only the quantity of current row => wrong total
document.getElementById('x_pre_approval').value += total; // Add quantity of current row to the existing value of the element #x_pre_approval => wrong result

arbei wrote:

You should get all quantities (not just the current row) and sum them up.


ebinnasir
User
Posts: 103

Post by ebinnasir »

Thanks for your suggesstion. I tried but don’t know how to create loop to get all quantities value instead of current row value.
Can you please help me to figure this loop.

Thanks in advance.

Emran


arbei
User
Posts: 9384

Post by arbei »

You may use jQuery (or JavaScript) to select the inputs for the quantities by the HTML attribute values. View HTML source to see the HTML attributes. Also read Inspect HTML Elements.


Post Reply