This project is read-only.

How to dynamically put calculated values in edit fields

Jun 3, 2013 at 8:11 AM
Hi Richard,

I want to dynamically put calculated values in edit fields.

For example - there will be three edit fields : Amount, Unit Price and Total Price. I want to dynamically put multiplication of Amount and Unit Price in Total Price field. How is it possible in naked objects framework? I am only able to put calculated value after pressing 'save' button.
But I want it will happen before pressing save button.

Jun 3, 2013 at 8:24 AM
I think the best approach with be to hook into the changed event of the relevant checkbox with jQuery and to do the calculations with JavaScript.
Jun 3, 2013 at 9:24 AM
... and if you want to keep DRY ("don't repeat yourself"), the Javascript could make a call to a companion Restful Objects server running the same backend domain object model. That way the calculation is still performed in your C# code.

(BTW, the RO code and NO code are now in the same code repository. I believe the idea is to amalgamate the two, though I don't know if that means having a single webapp running both NO and RO. If that is what we get then this use case becomes very easy to support).
Jun 3, 2013 at 1:51 PM

As Jacques has suggested, the only way to do this as described is to add some JavaScript.

But there may be other ways to achieve the real goal. You advised me that you want the user to be able to round the calculated total up or down.

Another option might be as follows:

Add another (optional) field called say Rounding that offers options: RoundUp, RoundDown, and NotApplicable (which is the default value).

Do a co-validation on the fields Amount, UnitPrice, and Rounding. In this validation method, check whether Amount * UnitPrice is an integer or not. If not, and Rounding still contains NotApplicable, then return a validation error that shows the calculated total and indicates that the user must in this case select RoundUp or Down. Then Total can be a non-editable calculated field, shown on save.

This is, admitedly, a slightly tortuous approach; doing the JavaScript might be simpler.