Fix #6464 - card header
[roojs1] / Roo / bootstrap / NumberField.js
index c61e9b6..e82d69f 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * - LGPL
  *
- * Input
- * 
+ * Number field 
  */
 
 /**
@@ -40,6 +39,11 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
      * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
      */
     allowNegative : true,
+    
+    /**
+     * @cfg {Boolean} allowZero False to blank out if the user enters '0' (defaults to true)
+     */
+    allowZero: true,
     /**
      * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
      */
@@ -62,23 +66,20 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
      */
     nanText : "{0} is not a valid number",
     /**
-     * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
-     */
-    castInt : true,
-    /**
-     * @cfg {Boolean} allowThousandsDelimiter (true|false) display thousands delimiter if true (e.g. "100,000") (defalut false)
+     * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
      */
-    allowThousandsDelimiter : false,
+    thousandsDelimiter : false,
     /**
-     * @cfg {String} symbol of thousandsDelimiter
+     * @cfg {String} valueAlign alignment of value
      */
-    thousandsDelimiter : ",",
+    valueAlign : "left",
 
     getAutoCreate : function()
     {
         var hiddenInput = {
             tag: 'input',
             type: 'hidden',
+            id: Roo.id(),
             cls: 'hidden-number-input'
         };
         
@@ -90,12 +91,12 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
         
         var cfg = Roo.bootstrap.NumberField.superclass.getAutoCreate.call(this);
         
+        this.name = hiddenInput.name;
+        
         if(cfg.cn.length > 0) {
             cfg.cn.push(hiddenInput);
         }
         
-        Roo.log(cfg);
-        
         return cfg;
     },
 
@@ -114,6 +115,10 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
             allowed += "-";
         }
         
+        if(this.thousandsDelimiter) {
+            allowed += ",";
+        }
+        
         this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
         
         var keyPress = function(e){
@@ -171,17 +176,31 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
 
     getValue : function()
     {
-        return this.fixPrecision(this.parseValue(Roo.bootstrap.NumberField.superclass.getValue.call(this)));
+        var v = this.hiddenEl().getValue();
+        
+        return this.fixPrecision(this.parseValue(v));
     },
 
     parseValue : function(value)
     {
+        if(this.thousandsDelimiter) {
+            value += "";
+            r = new RegExp(",", "g");
+            value = value.replace(r, "");
+        }
+        
         value = parseFloat(String(value).replace(this.decimalSeparator, "."));
         return isNaN(value) ? '' : value;
     },
 
     fixPrecision : function(value)
     {
+        if(this.thousandsDelimiter) {
+            value += "";
+            r = new RegExp(",", "g");
+            value = value.replace(r, "");
+        }
+        
         var nan = isNaN(value);
         
         if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
@@ -192,8 +211,24 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
 
     setValue : function(v)
     {
-        v = this.fixPrecision(v);
-        Roo.bootstrap.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
+        v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
+        
+        this.value = v;
+        
+        if(this.rendered){
+            
+            this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
+            
+            this.inputEl().dom.value = (v == '') ? '' :
+                Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
+            
+            if(!this.allowZero && v === '0') {
+                this.hiddenEl().dom.value = '';
+                this.inputEl().dom.value = '';
+            }
+            
+            this.validate();
+        }
     },
 
     decimalPrecisionFcn : function(v)
@@ -203,37 +238,11 @@ Roo.extend(Roo.bootstrap.NumberField, Roo.bootstrap.Input, {
 
     beforeBlur : function()
     {
-        if(!this.castInt){
-            return;
-        }
-        
         var v = this.parseValue(this.getRawValue());
-        if(v){
-            this.setValue(v);
-        }
-    },
-    
-    addThousandsDelimiter : function(v)
-    {
-        if(!this.allowThousandsDelimiter) {
-            return v;
-        }
-        
-        v += "";
-        
-        var x = v.split(".");
-        
-        var x1 = x[0];
         
-        var x2 = x.length > 1 ? "." + x[1] : "";
-        
-        var rgx = /(\d+)(\d{3})/;
-        
-        while (rgx.test(x1)) {
-            x1 = x1.replace(rgx, "$1" + this.thousandsDelimiter + "$2");
+        if(v || v === 0 || v === ''){
+            this.setValue(v);
         }
-        
-        return x1 + x2;
     },
     
     hiddenEl : function()