jqGrid Doesn't Sort When Showing Epoch Time (since as milliseconds) as Date

谁说我不能喝 提交于 2019-12-19 10:26:44

问题


I use jqGrid and my grid definition is like that:

...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date', 
  formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}], 
...

When I debug my coming data one of the date value (it is Number) is as follows:

1322550786997

Grid shows it like that:

29-Nov-2011

Everything is OK till this point. However when I want to sort my date column it doesn't change anything.

Any ideas?


回答1:


The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.

See the demo:

UPDATED: You can also use the following custom formatter as a workaround

formatter: function (cellval, opts) {
    var date = new Date(cellval);
    opts = $.extend({}, $.jgrid.formatter.date, opts);
    return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}

It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.



来源:https://stackoverflow.com/questions/8341824/jqgrid-doesnt-sort-when-showing-epoch-time-since-as-milliseconds-as-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!