2012-07-09 91 views
1

我有一個包含日期列的JQGrid。我使用日期格式化程序將日期格式化爲m/d/Y格式。以前,如果源日期的格式與我在formatoptions中傳遞的srcformat不匹配,則它不會格式化日期。 JQGrid v4.4.0現在嘗試格式化日期,無論源格式如何,並提供日期,這些日期都很合理:-)。JQGrid 4.4.0:'date'formatter srcformat/newformat現在試圖在源日期的格式不匹配時格式化日期格式

我已經填入此列的日期可能是正確的格式(m/d/Y),也可能是我在srcformatY-m-dTH:i:s)中定義的格式。

JQGrid 4.4.0有沒有辦法解析不符合srcformat的日期?


我列colModel DEF:

{name:"date", index:"date", label:"Date", width:85, jsonmap:"date", hidden: false, formatter:'date', sorttype: 'date', formatoptions:{srcformat:'Y-m-dTH:i:s', newformat:'m/d/Y'}, searchrules: { date: true } }   

回答

2

我解決我自己的問題:)

我創建了一個自定義的格式和所用的Datejs jQuery庫,以幫助在解析日期。

基本上格式化程序只把格式設置爲m/d/Y格式,如果它是Y-m-dTH:i:s格式;否則,它認爲它已經處於m/d/Y格式並且保持不變。

/** 
* This function formats the date column for the summary grid. 
* 
* The grid could be populated with dates that are in m/d/Y format or in Y-m-dTH:i:s format; need 
* to account for this; want the dates to end up being in m/d/Y format always. 
* 
* @param cellvalue  is the value to be formatted 
* @param options  an object containing the following element 
*      options : { rowId: rid, colModel: cm} where rowId - is the id of the row colModel is the object of the properties for this column getted from colModel array of jqGrid 
* @param rowObject  is a row data represented in the format determined from datatype option; 
*      the rowObject is array, provided according to the rules from jsonReader 
* @return    the new formatted cell value html 
*/ 
function summaryGridDateFormatter(cellvalue, options, rowObject) { 

    // parseExact just returns 'null' if the date you are trying to 
    // format is not in the exact format specified 
    var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss"); 

    // if parsed date is null, just used the passed cell value; otherwise, 
    // transform the date to desired format 
    var formattedDate = parsedDate ? parsedDate.toString("MM/dd/yyyy") : cellvalue; 

    return formattedDate; 
} 
0

@icats:thanks for this!它真的幫助 - 我開始變得非常沮喪...

我實際上已經修改你的函數了一下。 我從DB獲得時間戳字段,該字段通過JSON返回,作爲毫秒的值(即,表示實際值類似於:1343314489564)。因此,我爲parsedDate添加了第二項檢查,如下所示:

/** 
* This function formats the date column for the grid. 
* 
* The grid could be populated with dates that are in m/d/Y format or in Y-m-dTH:i:s format; need 
* to account for this; want the dates to end up being in m/d/Y format always. 
* 
* @param cellvalue  is the value to be formatted 
* @param options  an object containing the following element 
*      options : { rowId: rid, colModel: cm} where rowId - is the id of the row colModel is the object of the properties for this column getted from colModel array of jqGrid 
* @param rowObject  is a row data represented in the format determined from datatype option; 
*      the rowObject is array, provided according to the rules from jsonReader 
* @return    the new formatted cell value html 
*/ 
function dateFormatter(cellvalue, options, rowObject) { 

    // parseExact just returns 'null' if the date you are trying to 
    // format is not in the exact format specified 
    var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss"); 
    if(parsedDate == null) 
     parsedDate = new Date(cellvalue); 

    // if parsed date is null, just used the passed cell value; otherwise, 
    // transform the date to desired format 
    var formattedDate = parsedDate ? parsedDate.toString("yyyy-MM-dd HH:mm:ss") : cellvalue; 

    return formattedDate; 
}