1

I have a date column in my jqGrid that I am formatting. In addition, I have a toolbar search. The problem I am having is when I type in data to the search bar to search the formatted date data, it won't search the formatted date, it probably searches the pre-formatted date.

Is there any way to search the formatted date instead, or will I have to try and format it before I insert the data to the jqGrid?

Edit: Some Code

var gridLayout = [
        {name: "name", index: "name", width: "250px"},
        {name: "state", index: "state", width: "50px", hidden: true},
        {name: "stateName", index: "stateName", width: "100px", hidden: true,
            sorttype: function(cellValue, rowObj) {
                return rowObj.state;
            }},
        {name: "stateImg", index: "stateImg", width: "50px", align: "center",
            sorttype: function(cellValue, rowObj) {
                return rowObj.state;
            }},
        {name: "launchDate", index: "launchDate", width: "150px", sorttype: "date",
            formatter: 'date', formatoptions: {srcformat: "ISO8601Long", newformat:"n/j/Y, g:i:s A"}},
        {name: "lastWorked", index: "lastWorked", width: "150px",
            formatter: 'date', formatoptions: {srcformat: "ISO8601Long", newformat:"n/j/Y, g:i:s A"}}
    ];

I with the toolbar search above the launchdate/lastworked elements, I am trying to search for a date in the format specified in the "newformat" field, but it does not reply with any data when I do so.

After running a test, it does indeed search with the old format.

Edit:

I found an online example here: http://www.ok-soft-gmbh.com/jqGrid/MarkR.htm

If you enter 10/31/2007 into the date search bar and press enter, nothing appears. But if you type 2007-10-31 and press enter, the row with cell 10/31/2007 does appear in the search results. This is what I'm trying to work around.

Edit: This is jqGrid 4.4.0, jQuery 1.7.2

Joel Fischer
  • 6,521
  • 5
  • 35
  • 46

1 Answers1

2

jqGrid uses internal function parseDate during local searching/filtering. The implementation is very proprietary. I posted many bug fixes in the function. For example in the last one I suggested some changes to support j and n formats which you used. The bug fix is included in the current version 4.4.0 which you use.

Unfortunately the current version (4.4.0) of jqGrid still not supports the format g which you use. Only h format is currently supported. To have the format g supported one can for example include the lines

if(format[k] === 'g') {
    tsp.h = parseInt(date[k],10);
}

before the line with if(date[k] !== undefined) {.

How you can see on the demo the modified code will work:

enter image description here

The fixed version of jquery.jqGrid.src.js which I used in the demo you can get here.

UPDATED: After writing the answer I posted the following bug report to trirand. I wanted inform all that it's now included in the main code of jqGrid (see here). So the next version of jqGrid (the next after the version 4.4.0) will support the format g in the date.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you so much for the help and work you put into this. I do have another question. The way I have it set up, I wanted to search without having to put the full date in there (i.e. entering 10 would show all that start with 10). Since I am formatted by date, and not by, say, String, I am guessing that is why only if the full date/time is entered it will work? I tried using sopt: ['bw'] and that did not help either. Thanks again for all the help you have given. – Joel Fischer Aug 03 '12 at 18:26
  • And I pretty much just answered my own question, it has to be, equal, not equal, is in, is not in, etc. Thanks again. – Joel Fischer Aug 03 '12 at 18:30
  • @JoelFischer: You are welcome! If you want to implement some short form of the input of the date in the filter you can consider to use `beforeSearch` callback (see [the answer](http://stackoverflow.com/a/8953934/315935) for an example which could show you the implementation way). – Oleg Aug 03 '12 at 18:36
  • That example only seems to work with the filters themselves, and since the date only allows filters such as equals, not equals, is null, etc, I don't see how I could get a short form of input to work. I probably just don't know enough about the inner workings of jqGrid. I'm sure there's a way to use beforeSearch, but I think I'm just not knowledgeable enough. – Joel Fischer Aug 03 '12 at 19:12
  • @JoelFischer: I agree, that the rewriting of should date in the interval of dates is not very easy. It's important to understand that `postData.filters` can contains `groups` property additionally to `rules` property. If you would use `multipleSearch:true` together with and `multipleGroup:true` with [searching dialog](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching) the `postData.filters` will be set to the JSON format described [here](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching#options). So it's the most comon form supported by local filtering. – Oleg Aug 03 '12 at 19:49
  • @JoelFischer: The idea from [the answer](http://stackoverflow.com/a/8953934/315935) was to rebuild the simple searching filter produced as `postData.filters` by toolbar filtering in more complex form where `groups` part will be used. In the way one will be able to convert simple searching string from one field to multiple searching expressions. I did it inside of `beforeSearch` callback. – Oleg Aug 03 '12 at 19:52
  • The problem, again though seems to be that fields of type "date" are extremely limited in the types of searches that can be run on them, specifically, they cannot use begins with, and contains, which fail completely if I try to force it to use that search type. That seems to be the reason that I can't search on partial dates. If I had access to that column of data, I may be able to convert all of them to strings, and then search that way, but I still need to retain sort by date, which is more important than filtering by date at this time. – Joel Fischer Aug 03 '12 at 20:14
  • @JoelFischer: What I mean is that you can replace one search for dates equal to `10/4/2012` to *two* searches for the date which greater oe equal as `10/4/2012` and less then `10/5/2012` (`10/4/2012 <= x < 10/5/2012`). In the way you will replace one operation `"eq" 10/4/2012` to two operations connected with `AND`. One can implement such scenario mostly in the same way as in the example from [the answer](http://stackoverflow.com/a/8953934/315935). – Oleg Aug 03 '12 at 20:22
  • @JoelFischer: Just for info: the fix which I suggested here is already included in the main code of jqGrid (see **UPDATED**). So the next version of jqGrid which will be released will support "g" formatter in the date. – Oleg Aug 06 '12 at 09:34
  • yeah I thought about doing something like that, with a greater/less than search, but even just doing a simple single greater equal search with the format 10/4/2012 turns up no results. The only way to actually get a result using the formatted date is to have it correspond exactly while using the equals search type. Any idea why that might be? – Joel Fischer Aug 06 '12 at 16:36