-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi all
is there a way to enable the user to put , and . to set a number in a decimalbox ?
thanks
@guisimon,
It will need a special parser. Will need to distinguish 123,456,789 and 123,456.789, etc.
Until there is a clear spec. about the mixing of ',' and '.' in the number, it is hard to make this as an ZK generic solution...
In the mean time, you can use textbox and parse the input in the application...
@YADA
Have you running codes for such an input box for us?
thx
Stephan
There is no simple solution for this. The comma in the format pattern means "put the grouping symbol here"; it doesn't mean "use a comma".
Which symbol is used depends on the Locale or rather the DecimalFormatSymbols used in the DecimalFormat. You can specify your own; it you don't then the current locale will be used to determine the symbols.
That means the solution is to write your text input, install a converter and use that instead of Decimalbox. In the converter, create your own DecimalFormatSymbols and configure it with the symbols you want.
Note: This would be much easier if NumberInput.getRealSymbols() was protected...
This code seems to work with ZK 8:
import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.zkoss.json.JSONValue;
import org.zkoss.zul.Decimalbox;
public class SymbolDecimalbox extends Decimalbox {
private static final long serialVersionUID = 1L;
private DecimalFormatSymbols symbols;
private Locale locale;
/** Hide the parent method to make sure org.zkoss.zul.impl.NumberInputElement.getRealSymbols() is never called. */
@Override
public void setLocale(Locale locale) {
this.locale = locale;
}
/** Hide the parent method to make sure org.zkoss.zul.impl.NumberInputElement.getRealSymbols() is never called. */
@Override
public Locale getLocale() {
return locale;
}
public void setSymbols(DecimalFormatSymbols symbols) {
this.symbols = symbols;
}
public DecimalFormatSymbols getSymbols() {
return symbols;
}
//super//
@Override
protected void renderProperties(org.zkoss.zk.ui.sys.ContentRenderer renderer)
throws java.io.IOException {
super.renderProperties(renderer);
// By overriding setLocale(), we make sure org.zkoss.zul.impl.NumberInputElement.getRealSymbols() is never called.
renderer.render("localizedSymbols", getRealSymbols());
}
/** Send the specified symbols to the UI. */
private String getRealSymbols() {
if (symbols != null && locale != null) {
Map<String, String> map = new HashMap<String, String>();
map.put("GROUPING",
String.valueOf(symbols.getGroupingSeparator()));
map.put("DECIMAL",
String.valueOf(symbols.getDecimalSeparator()));
map.put("PERCENT", String.valueOf(symbols.getPercent()));
map.put("PER_MILL", String.valueOf(symbols.getPerMill()));
map.put("MINUS", String.valueOf(symbols.getMinusSign()));
final String localeName = locale.toString();
return JSONValue.toJSONString(new Object[] { localeName, map });
}
return null;
}
}
Asked: 2009-11-18 10:44:14 +0800
Seen: 1,512 times
Last updated: Jan 12 '17