我有一个有日期列的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 for column:
{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 } }
答案 0 :(得分: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;
}
答案 1 :(得分: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;
}