sortingdatejqgridjqgrid-php

jqGrid Sorting by date and time, but displaying only date


I have a column in my jqGrid that currently shows the date in 'd/m/y' format. It is formatted from a date and time string of the format 'Y-m-d H:i:s' and is sorted by date only. The current colModel is like the following:

{name: 'published_date', index: 'published_date', formatter: 'date', formatoptions: {newformat: 'd/m/y'}}

The sortname property is set to 'published_date'.

So this is working fine. What I'm trying to do now is sort with both the date and time because some of the rows that fall on the same date are in a seemingly random order. I still want to maintain the display format of 'd/m/y' but just sort it with the original date/time that it was formatted from.

I've tried some suggestions I've found, but none of them have worked for me yet. For example, someone suggested converting the date to an integer and trying to sort on that and then format it as 'd/m/y'. There also doesn't seem to be a sort field for datetime that works.

{name: 'published_date', index: 'published_date', sorttype: 'datetime', formatter: 'date', formatoptions: {newformat: 'd/m/y', srcformat: 'Y-m-d H:i:s'}}

If there aren't any built-in mechanisms in jqGrid to handle this, what would you suggest for a custom solution?

Using jqGrid 4.7.0, datatype is json and an example of the data from the server for published_date is "2015-03-04 18:38:12"


Solution

  • The option sorttype will be used by jqGrid only during local sorting. If you use datatype: "json" without loadonce: true then jqGrid just desplays one page of data sorted on the server side. If the user clicks on new page button or on the column header to sort the column then jqGrid will send new request to the server with new page, sidx and sord parameters. So the server will be responsible to sort the data correctly and to return the requested page of sorted data.

    I recommend in general to prefer to use loadonce: true in case of displaying no so large dataset (for example less as 1000 rows of data). The server should ignore page and rows parameters and to return all data back to jqGrid. One need just the sort the data initially. After the first loading the data jqGrid saved the data in data and _index parameters of jqGrid and changes the datatype to "local". The later sorting, paging or filtering/searching of data will be implemented by jqGrid without any communication with the server. One should clear understand that JavaScript code is relatively quickly in modern web browsers. Because of that the user will see the results of sorting, paging or filtering very quickly (practically immediately in case of small number of data). The rountrip time of the simplest request to the server is typically much longer.