JqGrid does not sort when displaying Epoch Time (since in milliseconds) as date

I am using jqGrid and my grid definition is as follows:

... colNames:['Type','Date','Message','User Name','Host'], colModel:[{name:'type',index:'type', width:100}, {name:'date',index:'date', sorttype:'date', formatter:'date', formatoptions: {newformat:'dM-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 data, one of the date values ​​(this number) looks like this:

 1322550786997 

The grid shows it like this:

 29-Nov-2011 

Everything is fine up to this point. However, when I want to sort my date column, it does not change anything.

Any ideas?

+2
source share
1 answer

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

See the demo :

enter image description here

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, 'dM-Y', opts); } 

It creates a Date , and then uses the original Date format to convert it to the 'dM-Y' format. See the demo here .

+4
source

Source: https://habr.com/ru/post/902000/


All Articles