-
2 Aug 2010 12:55 AM #1
Form field currency plugin
Form field currency plugin
Hi,
I couldn't find something like this so wrote it myself.
This currency plugin plugs in to a xtype: 'textfield' like so:
It kinda speaks for itself I guess.. Here's a screenprint of what the demo (included in zip) looks like if your run it.Code:{ xtype: 'textfield', width: 100, fieldLabel: 'Currency demo', plugins: 'currency', name: 'demo', currencyConfig: { symbolBeforeAmount: true, currencySymbol: '\u20AC', // = euro sign in unicode decimalSeparator: ',', thousandsSeparator: '.', negativeAmountClass: 'negativeAmount', invalidAmountText: 'Not a valid amount.' } }
currency600.jpg
The plugin creates a copy of the textfield with the same name (removes name property from original) so when you submit a currency field, the value is always sent as a float (submit decimal separator is a dot)
Two things to remember:
- the plugin only works on a Ext.form.TextField !
- use unique names for your textfields.
Update: Current version respects values set with the setValue() method. In other words, should work fine when (pre)loading a form with existing values from a backend for instance.
Last edited by realjax; 9 Aug 2010 at 4:27 AM. Reason: New version.
-
3 Aug 2010 8:08 AM #2
Hi,
your plugin works very well!
By the way, why are you cleaning the thousand separators? Removing and re-adding commas make the text shift and may not be good for the user experience.
-
3 Aug 2010 8:26 AM #3
I also think you should create a sequence on setValue() with updateShown(), or if I populate a form with setValues, the field is not formatted.
Then, you need to replace setValue() with setRawValue() in the plugin, and call updateHidden() after.
-
3 Aug 2010 8:44 AM #4
And probably would be a good idea to overwrite getValue(), making it to read from hiddenValue
-
3 Aug 2010 11:47 PM #5
True I need to make a few adjustments for when populating a form. Will do that in a day or two :-)
-
9 Aug 2010 5:13 AM #6
-
9 Aug 2010 6:22 AM #7
Executing your demo (reading from the second field):
Ext.getCmp('ext-comp-1451').getValue()
"12,000.00 $"
I expected to get "12000.00"
There are also problems if you use reversed separators and you call setValue().
For example (setting the first field),
set "E 10.000,00" in the field.Code:Ext.getCmp('ext-comp-1448').setValue('100.00')
I prefer to have field like jsakalos: you always set the value in a format and you always get it back in that format (see xdatefield, xtimefield, ...). I think it's much easier!
-
9 Aug 2010 11:48 PM #8
I'll fix things later for those of you who can't live without Ext.getCmp()

In the mean time, just get the fields from your formor read in the field by NAMECode:...getForm().getValues()
Should work fineCode:...getForm().findField('demo2')
-
9 Aug 2010 11:55 PM #9
Yes, I know that reading from form you get the unformatted value.
I used getCmp() because I also need to do some calculations in the form. I need to get the value for something else in the form.
I'm getting the field by name. But findField() has the same result of getCmp() - they both return the field - so field.getValue() will return the formatted value.
The point is that form.getValues() doesn't call getValue() on each field.
Probably is more wrong to override getValue(), probably would be better to have getUnformattedValue()
-
10 Aug 2010 12:55 AM #10
And what do you think about having a single "server format" to populate the field?
Similar Threads
-
Currency / money field with selectable currency
By simplessus in forum Ext 3.x: Help & DiscussionReplies: 3Last Post: 22 Jan 2011, 3:24 AM -
Personnal composite for currency field
By Kirua007 in forum Ext GWT: DiscussionReplies: 1Last Post: 23 Apr 2010, 4:34 AM -
[EXTJS 3.0.0] Currency Field Component : Setting value via Javascript
By JSCoder in forum Ext 3.x: Help & DiscussionReplies: 1Last Post: 8 Jan 2010, 1:39 AM -
Number Field with currency formatting
By JSCoder in forum Ext 3.x: Help & DiscussionReplies: 1Last Post: 2 Sep 2009, 9:39 AM -
How to format numeric field in form as US currency
By dlbjr in forum Ext 2.x: Help & DiscussionReplies: 2Last Post: 12 Feb 2008, 12:05 PM


Reply With Quote