2012-02-18 42 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