Ooops - my error. I need to do something like:
pcntDiscount.setModel(new Model(amount.getModelObject().toString())); Then the AjaxFormComponentUpdatingBehavior works as expected. Steve steve222 wrote: > > Many thanks for the detailed answer. > > I don't think the single field AbsoluteOrPercentage idea will work for me > without a lot of other changes. I use the same form for editing a record > as creating a new one. So I actually need to show both the > AbsoluteDiscount and PercentageDiscount on the form at the same time - > user enters % during initial entry, he needs to see % on later edit, etc. > Both fields need to be editable, with the one that doesn't get edited > being calculated. But thanks for the idea and the detail on how to > implement this. > > I've been attempting to do it with separate fields for AbsoluteDiscount > and PercentageDiscount - but it does not work for me. My model object > used in the form does not have setters for the calculated field > PercentageDiscount - just a getter to display the calculated % value. So > I can't update this with AjaxFormComponentUpdatingBehavior. > > And it seems I can't update the fields using AjaxEventBehavior, eg: > > final RequireTextfield amount = new RequiredTextfield("amount", > Double.class); > amount.add(DoubleMinimumValidator.minimum(1)); > amount.setOutputMarkupId(true); > container.add(amount); > > amount.add(new AjaxEventBehavior("onblur") { > protected void onEvent(AjaxRequestTarget target) { > > // this will always be null on new data entry, and old value > on edit > pcntDiscount.setModelObject(amount.getModelObject().toString()); > target.addComponent(pcntDiscount); > } > }); > > > > jWeekend wrote: >> >> Steve, >> >> If you prefer not to take the easy option and add a couple of radio >> buttons to let the user select currency or percentage input, that could >> also control a couple of labels (or a border) that shows, in a locale >> specific way, the currency symbol or the percentage sign you could ... >> create an AbsoluteOrPercentage class an instance of which will back the >> model of your DiscountAmount field. Make a validator that checks that the >> last character entered looks like an int or a '%' and that the other >> characters represent a number, and, if required, put your validation >> messages in the appropriate place. Add this validator to your >> DiscountAmount text field. Make an appropriate converter (implement >> IConverter - very simple logic required for both methods) and override >> getConverter on your text field to return it for your >> AbsoluteOrPercentage class. If this text field will be useful elsewhere, >> make a (top level) subclass of TextField - AbsoluteOrPercentageTextField >> for instance that does all of the above. >> >> It is not clear from your post what you want updated, but let's assume >> you have three fields, one for each of SalePrice, DiscountAmount and >> ActualAmount (price with discount applied) and you want the latter >> updated. Add an AjaxFormComponentUpdatingBehavior (probably "onblur") to >> both the input fields. In the onUpdate methods of each update the >> (dynamic) model object backing the ActualAmount field (probably rendered >> by a label since it is not meant to be edited by the user). Don't forget >> to add your "actualAmountTextField" component to the AjaxRequestTarget in >> both your onUpdate methods and also to request a markup id for >> actualAmountTextField - actualAmountField.setMarkupId(true) - where you >> build up the container. If this lot is likely to be useful elsewhere, >> wrap all three fields (and probably all the rest of the stuff described >> above) in a Panel so you can just drop it in a div anywhere you like >> later on. >> >> Does that do it? >> >> Regards - Cemal >> http://www.jWeekend.co.uk http://jWeekend.co.uk >> >> >> >> >> steve222 wrote: >>> >>> Hi. Hopefully I can explain this scenario OK - I'll try to keep it >>> simple. >>> >>> On a form, I have a couple of TextFields - SalePrice and DiscountAmount. >>> >>> After the user enters SalePrice, I need a way to allow the user to enter >>> the DiscountAmount as either a flat rate or as a percentage of the >>> SalePrice. >>> >>> For example, if entered SalePrice is 1000, the user could enter a flat >>> DiscountAmount of 200.00. Or they could enter 20%. I'd like the option >>> to do either since SalePrice might be 8,745 and the discount 2.75% or >>> something (so hard to just calculat the true cash amount). Or it might >>> just really be a flat £200 in which case hard to calculate the %. >>> >>> In a traditional app where I'm writing the JavaScript, I can think of >>> various ways to do this involving extra fields for flatrate and/or % - >>> then updating the real DiscountAmount field when the "dummy" fields >>> change. >>> >>> But I'm trying to work out an elegant way with Wicket/AJAX - without too >>> much success so far. >>> >>> Any suggestions - or links to similar examples? Ideally, the fewer >>> extra dummy fields the better. >>> >>> Thanks. >>> >>> >>> >> >> > > -- View this message in context: http://www.nabble.com/Wicket-AJAX-update-form-TextField-value-from-another-field-tp20117258p20120995.html Sent from the Wicket - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]