0

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"

The Unknown Dev
  • 3,039
  • 4
  • 27
  • 39
  • which version of jqGrid/free jqGrid/Guriddo jqGrid JS you use? Which `datatype` you use? `sorttype` works only with the *local* data or the data loaded from the server with additional option `loadonce: true`. Which exact format have the data? Could you provide the text data for `published_date` which you use. – Oleg May 07 '15 at 16:52
  • Do you use `datatype: "json"` **without** `loadonce: true`? In the case jqGrid don't sort at all and `sorttype` will be ignored. Instead of that jqGrid send new request to the server with new value of `sidx` and `sord`. **The server have to return the page of sorted data**. – Oleg May 07 '15 at 18:09
  • @Oleg Thank you. I am not using `loadonce` which would explain why the sorting had no effect from jqGrid. I'm now playing around with the sorting on the server side and that seems to have some effects. – The Unknown Dev May 07 '15 at 18:22
  • It's very important that the server returns **all data** instead of one page of data in case of usage `loadonce: true`. By the way I recommend to use `loadonce: true` in general if you have not so large dataset. For example in case of less as 1000 rows of data it's better to use `loadonce: true`. The main reason that local sorting and filtering of data *by web browser* is much more quickly as sending a simple network request to the server and to getting the response back. Moreover you will have really simple server code in case of usage `loadonce: true` scenario. – Oleg May 07 '15 at 18:28

1 Answers1

2

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798