Shaggy
Shaggy

Reputation: 315

jqGrid Date column sort does not work with sorttype:'date'

I am using jqGrid in MVC 4 to display data. I am having a grids with date/ datetime column in it. I am returning C# DateTime variable data for grid date column; which is displaying fine with the formatoption: {srcformat: 'm/d/Y', newformat: 'm/d/Y'} for date column & formatoption: {srcformat: 'm/d/Y h:i:s', newformat: 'm/d/Y h:i:s A'} for datetime column. but the sorting of this column does not reflect when user clicks column header. here is the colModel for date column

{
    name: 'TestOrderDate', index: 'TestOrderDate', formatter: 'date',
    sorttype: 'date', width: 90, align: 'center', fixed: true,
    formatoptions: { srcformat: 'm/d/Y', newformat: 'm/d/Y' }
},

& for datetime column

{
    name: 'TestOrderDate', index: 'TestOrderDate', formatter: 'date',
    sorttype: 'date', width: 90, align: 'center', fixed: true,
    formatoptions: { srcformat: 'm/d/Y h:i:s', newformat: 'm/d/Y h:i:s A' }
},

Can anybody tell me what is wrong in colModel due to which sort behavior not working.

Upvotes: 2

Views: 7379

Answers (3)

Dave
Dave

Reputation: 1

My fix is to trick jqGrid to load an int but display a date.

my colModel is {name: 'startDt', index: 'startDt', width: 50, sortable: true, sorttype: 'int', formatter: 'date', formatoptions: {srcformat: "U", newformat: "d-M-Y"}},

When you load the grid data, convert your date string to milliseconds divided by 1000. So in my case startDt is a very large integer value.

If your date is a blank/null, just load the grid with ''.

What's nice is that the blank/null is now sorted properly. Ascending has blank/null(s) first, Descending has blank/null(s) last.

Upvotes: 0

Nilesh
Nilesh

Reputation: 41

jqGrid does not support sorting by datetime but by just date. Hence you can use alternative as given in its PDF i.e. shown in below example. If your data in grid is already sorted by Date and Time , and you also have a column with the Index of all rows like in Numbers as 1, 2, 3, .... n . The you can sort the datetime on the Index Column. This will always ensure asc or desc order for datetime.

You can sort the jqGrid's DATE and time or date by another Column content. Such as given in example below on onSortCOl:

            onSortCol: function(name,index) { if(name == 'createDateTime') { jQuery("#viewNotesGrid").setGridParam({sortname:"ID"}); } }

Upvotes: 3

Oleg
Oleg

Reputation: 221997

I recommend you update 2.5 years old version 4.1.1 to version 4.5.4. It contains new implementation of date parsing. To sort the column having sorttype:'date' option jqGrid first parse (unformat) the string displayed in the grid. Formats like 'm/d/Y h:i:s A' not supported for parsing (and sorting) in old versions of jqGrid.

The versions of jqGrid starting with 4.3.2 don't need the file ellipsis-xbl.xml. It was used in early versions to display ellipsis in old versions of Mozilla/Firefox browsers. The line of ui.jqgrid.css was replaced to the line starting with 4.3.2.

Upvotes: 0

Related Questions