1

我有一个具有日期列的 JQGrid。我使用日期格式化程序将日期格式化为某种m/d/Y格式。srcformat以前,如果源日期的格式与我传入的不匹配formatoptions,它就不会格式化日期。无论源格式如何,JQGrid v4.4.0 现在都尝试格式化日期,并提出了很远的日期:-)。

我填充到此列中的日期可能已经采用正确的格式 ( m/d/Y),也可能采用我在srcformat( Y-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 } }        
4

2 回答 2

2

我解决了我自己的问题:)

我创建了一个自定义格式化程序并使用Datejs JQuery 库来帮助解析日期。

基本上,格式化程序仅将日期格式化为 m/d/Y 格式(如果它是 Ym-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;
}
于 2012-07-10T22:04:21.290 回答
0

@icats:谢谢!它真的很有帮助 - 我开始变得非常沮丧......

实际上,我不得不稍微修改一下您的功能。我从数据库中获取了一个时间戳字段,该字段通过 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;
}
于 2012-07-31T13:20:25.953 回答