2012-02-18 68 views
4

我用Ext.form.DateField与指定的格式是“DM-Y”,像这样:的ExtJS的DateField混淆格式

var sellingDate = new Ext.form.DateField({ 
    fieldLabel : "Selling date", 
    width : 180, 
    name: 'sellingDate', 
    format: 'd-m-Y', 
    allowBlank : false 
}); 

我想这个组件将用给定的格式自动完成输入值也就失去后焦点。我的意思是如果我输入文本'040212'(2012年2月4日,在我的国家我们使用'dd-mm-YYYY'作为标准日期格式),它必须显示该文本为'04-02-2012'。
但是,当在DateField的事件“更改”处进行调试时,我看到解析的Date对象是“Mon Apr02 2012”。我不知道如何让它像我期望的那样工作。有没有办法从日期字段获取原始文本,而不是解析的日期对象?你能帮我解决这个问题吗?
非常感谢!

回答

0

无论您指定的格式如何,Ext都会将任意六位数的字符串解析为mmddyy。这是不幸的: -/

解析日期的逻辑发生在方法的Ext.form.field.Date或(Ext.form.DateField在ExtJS 3中)。你可以在“拦截”这个方法,它解析日期之前执行自己的原始值的按摩:

Ext.onReady(function(){ 
    (function(){ 

    // capture the original method so we can call it later 
    var originalBeforeBlur = Ext.form.field.Date.prototype.beforeBlur; 

    Ext.override(Ext.form.field.Date, { 
     beforeBlur: function(){ 
     var raw = this.getRawValue(), 
      match = raw.match(/^\s*(\d{2})(\d{2})(\d{2})\s*$/); 

     // rearrange the date string to match what Ext expects 
     if (match){ 
      raw = match[2] + match[1] + match[3]; 
     } 

     this.setRawValue(raw); 
     originalBeforeBlur.call(this); 
     } 
    }); 
    })(); 

    var panel = new Ext.form.FormPanel({ 
    title: "Enter a Date", 
    renderTo: Ext.getBody(), 
    items: { 
     xtype: "datefield", 
     format: "d-m-Y" 
    } 
    }); 
}); 
1

这很简单,添加altFormats配置选项:

altFormats:字符串多个日期格式用“|”分隔尝试当 分析用户输入的值,它不符合定义的格式

//So in your case, it should be 'dmy' 
var sellingDate = new Ext.form.DateField({ 
    fieldLabel : "Selling date", 
    width : 180, 
    name: 'sellingDate', 
    format: 'd-m-Y', 
    altFormats: 'dmy', 
    allowBlank : false 
}); 
+0

那不适合我 – Duleep 2013-11-13 10:11:10

+0

它的工作的工作,这就是altFormats是。 – 2014-10-19 19:28:26