0

This is one of the columns in my grid-

{ name: 'DueDate',
            index: 'DueDate',
            sortable: true,
            sorttype: 'date',
            editable: false,
            width: 125,
            formatter: 'date',
            searchoptions: {
                dataInit: function(element) {
                    $(element).datepicker({
                       dateFormat: 'mm/dd/yy'
                    });
                }
            }
        }

I am trying to setup a custom filter like this-

 var now = moment();
    var formattedDate = now.format('MM/DD/YYYY');
    var date = Date.parse(formattedDate);
    var f = { groupOp: "OR", rules: [] };
    f.rules.push({ field: 'DueDate', op: 'lt', data: date});
    $grid[0].p.search = true;
    $.extend($grid[0].p.postData, { filters: JSON.stringify(f) });
    $grid.trigger('reloadGrid');

'DueDate' returned is a date string. so i need to parse it before i can compare. How do i do this? Is there even a way to do it?

UPDATE: Data Sample from controller

DueDate is a Datetime object and the value from the controller is /Date(-62135568000000)/(json object) This is formatted into 'mm/dd/yy' format and this converts to a date string and looks like normal date. I have a drop down menu item with all the filter criteria. I click on one of the menu-items, which is to select all the rows that have DueDate < Today's date for which i need to parse 'DueDate' field's value.

For Testing, when i use this-

 var now = moment();
    var formattedDate = now.format('MM/DD/YYYY');
    var rowdata = $grid.jqGrid('getRowData');
    var dueDate = rowdata[0].DueDate; //just picking first row's due date
    alert("DueDate: " + dueDate);//alerts "1/1/2015"   
    var d = Date.parse(formattedDate);
    //var ts = dueDate.getTime(); --I get an error here- getTime() not supported for this object.
   //this works fine- & I need something like this for filters
    if (Date.parse(dueDate) < d) {
        alert("It is due");
    }
    var f = { groupOp: "AND", rules: [] };
    f.rules.push({ field: 'DueDate', op: 'lt', data: d });
    f.rules.push({ field: 'HasMapping', op: 'eq', data: true });
    $grid[0].p.search = true;
    $.extend($grid[0].p.postData, { filters: JSON.stringify(f) });
    $grid.trigger('reloadGrid');
HappyLee
  • 435
  • 4
  • 11
  • could you post an example of input data for `DueDate` column? It's very strange that you don't use any `srcformat` and `newformat` properties of the `formatoptions`. Are the settings from you locale corresponds the format of data which you use? Which version of jqGrid you use and from which fork of jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7) – Oleg Apr 28 '16 at 21:02
  • @Oleg, I am using jqGrid - 4.6.0.Example for duedate input data - "04/28/2016" this is how it looks like. This filter function works fine with all the columns except the date columns. – HappyLee Apr 28 '16 at 21:08
  • could you post an example of input data for DueDate column? Do you have the demo which can be used to reproduce the problem? – Oleg Apr 28 '16 at 21:11
  • @Oleg, unfortunately, i dont have a demo to show you. Please see the updated section in the question. – HappyLee Apr 28 '16 at 21:32
  • @Oleg, I also referred your answer to this link - http://stackoverflow.com/questions/28599002/jqgrid-filter-rules-can-we-filter-based-on-an-array Do you think this works in my case? – HappyLee Apr 28 '16 at 23:25
  • Using free jqGrid you can have full control on sorting and filtering, but you wrote before about the usage of jqGrid 4.6. [The old demo](http://www.ok-soft-gmbh.com/jqGrid/OK/DatetimeSearch1.htm) for example use custom filtering of dates where by filtering on "equal to" `4/15/2015` one will find the date like `4/15/2015 3:31:49 PM`. Moreover free jqGrid supports more date formates, like `"u1000` together with old `"u"`. – Oleg Apr 28 '16 at 23:37
  • @Oleg, oh i didn't know that example used free jqGrid. I guess then I don't see a way to update field value before setting in the rule. – HappyLee Apr 28 '16 at 23:40
  • I develop free jqGrid fork after changing license agreement in jqGrid, making it commertial and renaming it to Guriddo. I implemented many new features. – Oleg Apr 28 '16 at 23:44
  • Oh okay. Yes your answers have always helped me the most. Unfortunately, we can't use guriddo now, as we use jqgrid company wide. I am still looking for options to some how set the field value before setting the rule :( – HappyLee Apr 28 '16 at 23:47
  • I don't suggested you to use Guriddo. On the opposite I wrote that there are now **two different forks:** free jqGrid (the current version 4.13.2), which I develop and *another fork* Guriddo jqGrid JS (the current version 5.1.0). If you do need to hold some retro version then you should create the demo (in JSfiddle, for example), which uses some test data and use unminimized version of jqGrid. You should describe step by step the test case to reproduce the problem, which you report. – Oleg Apr 29 '16 at 04:57

0 Answers0