jqGrid: client side sort doesn't work with dates!
I have a date column in a jqGrid with colOption for that column defined as:
{name:'LTD', index:'LTD', width:110, align:'right', sortable:true, sorttype:'date', formatoptions:{srcformat:'Ymd', newformat:'d/m/Y'}, formatter: 'date'},
The JSON data returned from the server has '20101231', etc. in the field, and it gets displayed properly (that is, it's displayed like '31/12/2010'). However, clicking in the column header to sort it, does nothing!
The only way I could get the sort working was to not change the srcformat (that is, accept the default of 'Y-m-d'). However, it doesn't work with any other custom format.
An additional issue is with the search on this column. No matter how I specify the value in search popup (with eq开发者_如何学JAVAual as operator), it doesn't find the record!!
jQuery : 1.4.4
jqGrid: 3.8.2
What could be the reason?
The format option where srcformat
or newformat
has format 'Ymd' is not supported by 'date'. If must be a string which contain separators between 'Y', 'm' and 'd'. So I strictly recommend you to change your server code to produce date in the ISO 8601 Date format 'Y-m-d'.
If you can not change the code of the server you will have to use custom formatter to be able to display the date in the newformat:'d/m/Y'. The following parameters for the date column can you use instead of formatoptions:{srcformat:'Ymd', newformat:'d/m/Y'}, formatter: 'date'
:
sorttype: 'date', datefmt:'d/m/Y',
formatter: function (cellvalue, options, rowObject) {
var y = Number(cellvalue.substring(0,4));
var m = Number(cellvalue.substring(4,6));
var d = Number(cellvalue.substring(6,8));
return String(d)+'/'+String(m)+'/'+String(y);
}
You will be able to display and make local sort of the data, but for the searching of data you will have to use the source format "20101016" if you want to search for the date which will displayed as "16/10/2010". See small demo here.
精彩评论