SInce both the detail form and master form are all available on the same page, you need to create a javascript fucntion which when called can LOOP throught all detail rows as it sums up the values from each to get the overall toatl, after which it can assign it to a field on your master form.
Then attch event handlers to the fields in your detail table so that they call the above function everytime they change thier values, this will give you the desired effect
THIS IS FROM AN OLD TOPIC BUT ITS CA POINT YOU IN THE RIGHT DIRECTION.
THE FULL TOPIC IS AT viewtopic.php?f=4&t=40703 so you can read through it all BUT IN A SUMMARY below if the approach you want, just read through it shoud give you an idea of how to do what i indicated above
In my case I have the following tables
table(1) product:id,name
table (2)sale_detail:id,product_id,quantity,price,total_amount
2.allow user to put type in quantity & price for any of the rows, and i use that to calculate the total_amount on each row.
3.Be able to display the sum of all total amounts(net_amount) somewhere else everytime the total_amount of any of the rows changes.
5.Attach scripts to auto re calculate the total_amount for every row when quantity or price has changed, got to field settings of quantity&price in sale_detail->client side events and put
{ // keys = event types, values = handler functions
"keyup": function(e) {
// Your code
var $row = $(this).fields();
var total_amount = $row["quantity"].toNumber() * $row["price"].toNumber();
//update total amount for this row
if((total_amount > 0) || (total_amount < 0))
{$row["total_amount"].value(total_amount);}
else
{$row["total_amount"].value("0");}
//recalculate net amount
CalculateNet();
}
}
NOTE: notice the function "CalculateNet()" it is the one responsible for recalculating the net amount so we call it after we are sure total_amoutn on a given row has changed
- put the CalculateNet() function in client scripts->table specific->add section so that its available to be called. SO paste the function as below
NOTE: if you inspect your a total_amount input field you'll see is has certain properties id="x[n]_total_amount" where n is the row number, class="form-control" and data-field="x_total_amount", so we just have to look for these controlls and get thier values and sum them up
function CalculateNet(){
var net_amount = 0;
var all_amounts = $('[data-field="x_total_amount"]');
for(var i = 0;i < all_amounts.length;i++){
var the_amount = 0;
//ensure that its the input controll of interest
if(all_amounts[i].className == "form-control"){
the_amount = all_amounts[i].value;
if(!(the_amount > 0)){the_amount = 0;}
net_amount += parseFloat(the_amount);
}
}
//show the net amount anywhere for now am putting in console log
console.log(net_amount);
}
- if you've done it correctly you should see the net_amount in console log change every time you change the qauntity/price on any row. now you just have to draw a row below to show it or something.