I don't think this is possible but my client asked so…
As I add a new item to a stock database there's a field for what the item cost me, another field for the price I sold it for. Can I show a profit field within that Add (or edit) dialogue but which updates within the dialogue box as soon as I've entered the cost and sale prices rather than only on the report after I hit submit?
Hello,
Refer faq: How to populate other column, based on previous columns.
It uses onblur event to calculate other field.
Thanks – that almost works, however in the Add/Edit dialogue boxes the calculation result is NaN. I've tried removing the currency symbol and making the purchase and sale fields numeric rather than currency but still NaN
Relevant code below:
$col = array();
$col["title"] = "Cost";
$col["name"] = "PurchasePrice";
$col["width"] = "16";
$col["formatter"] = "number";
#$col["formatter"] = "currency";
#$col["formatoptions"] = array("prefix" => "£");
$col["editable"] = true;
$col["editoptions"] = array("onblur" => "update_field()");
$col["search"] = true;
$cols[] = $col;
$col = array();
$col["title"] = "Price";
$col["name"] = "SalePrice";
$col["width"] = "16";
$col["formatter"] = "number";
#$col["formatter"] = "currency";
#$col["formatoptions"] = array("prefix" => "£");
$col["editable"] = true;
$col["search"] = true;
$col["editoptions"] = array("onblur" => "update_field()");
$cols[] = $col;
$col = array();
$col["title"] = "Profit";
$col["name"] = "Profit";
$col["width"] = "14";
$col["formatter"] = "number";
#$col["formatter"] = "currency";
#$col["formatoptions"] = array("prefix" => "£");
$col["editable"] = true;
$col["on_data_display"] = array("do_maths","");
function do_maths($data){
return $data["SalePrice"] – $data["PurchasePrice"] ;}
$cols[] = $col;
function update_field()
{jQuery('input[name="Profit"]:visible').val( parseFloat(jQuery('input[name="SalePrice"].editable').val()) – parseFloat(jQuery('input[name="PurchasePrice"].editable').val()
) ); }