jqGrid不支持Date
作爲比較操作中的本機數據類型,所以我建議你採用兩種方法作爲解決方法。
1)您可以使用sorttype
作爲函數。在使用Date
參數調用函數的情況下,該函數可以返回可以使用的字符串而不是比較操作中的日期。例如
sorttype: function (d) {
if ($.isFunction(d.toISOString)) {
return d.toISOString();
}
return ISODateString(d);
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
function ISODateString(d) {
function pad(n) { return n < 10 ? '0' + n : n; }
return d.getUTCFullYear() + '-'
+ pad(d.getUTCMonth() + 1) + '-'
+ pad(d.getUTCDate()) + 'T'
+ pad(d.getUTCHours()) + ':'
+ pad(d.getUTCMinutes()) + ':'
+ pad(d.getUTCSeconds()) + 'Z'
}
}
2)可以延伸通過的jqGrid內部使用,以支持Date
類型_compare功能。您可以使用我在this old answer中描述的技巧。在_compare
使用的情況下,代碼將
var oldFrom = $.jgrid.from;
$.jgrid.from = function (source, initalQuery) {
var result = oldFrom.call(this, source, initalQuery),
old_compare = result._compare;
result._compare = function (a, b, d) {
if (typeof a === "object" && typeof b === "object" &&
a instanceof Date && b instanceof Date) {
if (a < b) { return -d; }
if (a > b) { return d; }
return 0;
}
return _compare.call(this, a, b, d);
};
return result;
};
可以jqGrid的的使用之前插入代碼,就像我展示它the demo。
已更新:我發佈了the pull request解決了這個問題。
我發佈了可以解決問題的池請求(請參閱我的更新回答)。 – Oleg 2012-03-25 11:16:01