Page 1 of 1

Detail Table Field value Summation

Posted: Fri Feb 02, 2024 4:19 am
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


Re: Detail Table Field value Summation

Posted: Fri Feb 02, 2024 9:44 am
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.

Re: Detail Table Field value Summation

Posted: Fri Feb 02, 2024 6:05 pm
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


Re: Detail Table Field value Summation

Posted: Fri Feb 02, 2024 7:16 pm
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.

Re: Detail Table Field value Summation

Posted: Thu Feb 29, 2024 7:11 am
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.


Re: Detail Table Field value Summation

Posted: Thu Feb 29, 2024 8:55 am
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.


Re: Detail Table Field value Summation

Posted: Thu Feb 29, 2024 4:56 pm
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


Re: Detail Table Field value Summation

Posted: Thu Feb 29, 2024 5:05 pm
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.