tags:

views:

182

answers:

1

Hi there,
I want to allow users enter numbers in textfield and once the textbox loses focus. The number is formatted with commas.

e.g. User enters 100000
textfield looses focus
value displayed: 100,000

How can I achieve this in Grails.

I have looked at

<g:formatNumber number="${myNumber}" format="\\$###,##0" />

But it doesnt solve my problem as the number is from a textfield.

thanks

Much appreciated.

+1  A: 

Well, you have to understand the difference between code that is executed on the server-side vs. client-side scripting. Your requirement "once the textbox loses focus" just can't be met by Grails, because detecting focus loss of DOM elements and reacting to it happens entierely in the user's browser. You will have to do this in JavaScript (which isn't a big deal either). For example these two jQuery plugins should do the trick (but there are plenty of other solutions, also for other JS libraries).

Daniel Rinser
Thanks Daniel.I understand what you mean here.Well I noticed when a property say Cost is declared as long in the domain. Grails automatically adds the formatting logic.Which implies: When a user enters a number(including commas) in the textfield, Grails automatically strips the commas to store just the number in the db. Also, when the same field is displayed as a label, Grails automatically adds commas to it.I dont know how it works in the background. I want to have similar kind of functionality.
WaZ
I understand, that you want to format the (numeric) input of an HTML text field as soon as the user leaves that text field. The point I'm trying to make is, that this functionality can only be achieved with client-side scripting, ie. JavaScript. Grails has nothing to do with this. Have a look at JavaScript, jQuery and the two jQuery plugins I've linked in my answer.
Daniel Rinser
Hi Daniel, I know what you mean my client side code and server side. If you declare a property as Long in grails you will notice grails does that automatically.Please let me know when you try it out.Thanks.
WaZ