Hi Martin,

I've solved the problem with the NumberFormat class. I was making and error.
So I've implemented a little TextField widget.

But I've found two problem i guess. It seems that the regexp expression is
checked only on the input and not on the content in the TextField. I don't
know if it is suppose to be like this or not.

Another fact is that there's a strange error. With the NumberFormat class if
i try to put the update formatted content in the TextField sometimes it
doesn't work.

I'm checking the "input" event. So when the input change i update the value
property of the TextField like this:

this.formatter = new qx.util.format.NumberFormat("it");

var parsed = this.formatter.parse(data.getData());
this.setValue(this.formatter.format(parsed));

If the number on the field is without dots, like this: 100
If i press another key (like a zero for example) i get the correct answer:
1.000
But now if i add another number i got this: 10000 instead of 10.000
Now if i press another number i got this: 100.000

It seems that when a dot is in the content the format is not applied.

This code can partially replicate the error. The commas remains instead of
the dots because i cannot define a locale in the config.json of the
playground, also with the commas the behaviour is weird but with another
type of error. The commas are misplaced even more than with the dots.

// Create a button
var field = new qx.ui.form.TextField();

field.setFilter(/(\d|,)/);

// Document is the application root
var doc = this.getRoot();

// Add button to document at fixed coordinates
doc.add(field,
{
  left : 100,
  top  : 50
});

var formatter = new qx.util.format.NumberFormat("it");

// Add an event listener
field.addListener("input", function(data) {
  
  var parsed = formatter.parse(data.getData());
  field.setValue(formatter.format(parsed));
  
});

I've made two methods to perform parsing and formatting manually, but i got
the same problem as using the NumberFormatter, when there are no dots all
perform well, with dots the number is not formatted.

This is the mixin i've written:

qx.Mixin.define("mx.ui.form.MCommercialFormatter",
{
        properties :
        {
                /**
                 * The separator used in the grouping.
                 * 
                 * @type String
                 */
                separator :
                {
                        check : "String",
                        init : ".",
                        nullable : false
                },
                
                /**
                 * The suffix to add after the number.
                 * 
                 * @type String
                 */
                suffix :
                {
                        check : "String",
                        init : null,
                        nullable : true
                }
        },
        
        members :
        {
                /**
                 * Format a string into a formatted string.
                 * 
                 * @param string {String} The string to format.
                 * 
                 * @return {String} The formatted commercial string.
                 */
                group : function(string)
                {
                        var arr = qx.lang.String.toArray(string);
                        var l = arr.length;
                        var formatted = "";
                        
                        for(var i = 1; i <= l; i++)
                        {
                                if(i % 3 == 0 && (l - i) != 0)
                                {
                                        //console.log("i = "+i);
                                        //console.log("i % 3 = "+(i % 3));
                                        qx.lang.Array.insertAt(arr, 
this.getSeparator(), l - i);
                                }
                        }
                        
                        for(var i = 0, l = arr.length; i < l; i++)
                                formatted += arr[i];
                        
                        return formatted;
                },
                
                /**
                 * Parse a commercial formatted string and return a string 
without dots or
commas.
                 * 
                 * @param string {String} The string to parse.
                 * 
                 * @return {String} The parsed string cleared from dots and 
commas.
                 */
                parse : function(string)
                {
                        var arr = qx.lang.String.toArray(string);
                        
                        for(var i = 0, l = arr.length; i < l; i++)
                        {
                                if(arr[i] == this.getSeparator())
                                {
                                        qx.lang.Array.remove(arr, arr[i]);
                                }
                        }
                        
                        console.log(string);
                        
                        return string;
                }
        }
});

-----
La coincidenza non ha madre.
--
View this message in context: 
http://qooxdoo.678.n2.nabble.com/Number-formatting-and-separators-tp6776330p6783821.html
Sent from the qooxdoo mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Doing More with Less: The Next Generation Virtual Desktop 
What are the key obstacles that have prevented many mid-market businesses
from deploying virtual desktops?   How do next-generation virtual desktops
provide companies an easier-to-deploy, easier-to-manage and more affordable
virtual desktop model.http://www.accelacomm.com/jaw/sfnl/114/51426474/
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to