Show calculated profit in Add or Edit dialogue box

QuestionsShow calculated profit in Add or Edit dialogue box
william hindle asked 9 years ago

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?

3 Answers
Abu Ghufran answered 9 years ago

Hello,

Refer faq: How to populate other column, based on previous columns.
It uses onblur event to calculate other field.

How-Tos & FAQs

william hindle answered 9 years ago

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()
) ); }

Abu Ghufran answered 9 years ago

Hi,

Code looks fine. I'd recommend to debug values using alert() or console.log.
If code is online, you can email link for review.

Your Answer

18 + 19 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?