We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to set initial grid filter for null date column?

I'm trying to set an initial grid filter on page load, so that the DataManagerRequest in the Controller will filter the results for 2 columns. We do this in other places and it works fine, this is the first time I've tried to do it for a null date and can't get it to work.  We're using EJ2 grids in an ASP.NET application, Syncfusion version 17.1.50

The page is like:
@{
    List<object> filterColumns = new List<object>();
    filterColumns.Add(new { field = "TaskStatus", matchCase = false, @operator = "notequal", predicate = "and", value = 9 });
    filterColumns.Add(new { field = "DueDate", matchCase = false, @operator = "notequal", predicate = "and", value = (DateTime?)null });
}

<div>
        @(Html.EJS().Grid<CaseServiceTaskVm>("MyTasksGrid")
              .DataSource(ds => { ds.Url(Url.Action("LoadRecords", "Tasks")).Adaptor("UrlAdaptor"); })
              .AllowFiltering().FilterSettings(filter => filter.Columns(filterColumns).Type(FilterType.Menu))
              .Columns(col => {
                  col.Field(p => p.Id).IsPrimaryKey(true).IsIdentity(true).Visible(false).Add();
                  col.Field("CaseNumber").Width(100).HeaderText("Case ID").ValueAccessor("formatCaseNumber").Add();
                  col.Field("TaskStatus").HeaderText("Status").Width(100).Type("string")
                      .Filter(new { type = "CheckBox" }) // for FilterType.Menu
                      .ForeignKeyField("value").ForeignKeyValue("text").DataSource((IEnumerable<object>)ViewData["TaskStatus"]).Add();
                  col.Field("DueDate").HeaderText("Due Date").Width(100).Type("date").Format("yMd").Add();
              })
              .Render())
</div>

The Controller method is declared as:
public ActionResult LoadRecords(DataManagerRequest dataManager) {
 [...]
}

While debugging the LoadRecords method, if I manually filter by DueDate it comes across in the DataManagerRequest with 2 predicates for Where[0], the first is TaskStatus as expected, and the 2nd predicate is Field = "DueDate", Operator = "notequal", value = null.

When I try to do it in code as described above, it also comes through in the DataManagerRequest with 2 predicates for the Where[0], the first is correct TaskStatus (same as above), but the 2nd predicate is blank with 2 child predicates - [0] is field = DueDate, Operator = "lessthanorequal", value = null, and the 2nd is field = "DueDate", Operator = "greatherthanorequal", value = null.

I can't figure out how to get the date to come through correctly programmatically, the same way it would work from the filter menu.  Any help would be appreciated.

Thanks,
Rich W.

2 Replies

TS Thavasianand Sankaranarayanan Syncfusion Team September 4, 2019 09:07 AM

Hi Richard, 
 
Thanks for contacting Syncfusion support. 
 
We have validated the provided code example and reported issue and we found the root cause of issue the filter value is not properly set for Date column(the null value changed as undefined while serializing  ). So we have achieved your requirement in the below way. Please find the code example for your reference. 
 
 
@{ 
    List<object> filterColumns = new List<object>(); 
    filterColumns.Add(new { field = "EmployeeID", matchCase = false, @operator = "notequal", predicate = "and", value = 4 }); 
    filterColumns.Add(new { field = "ShippedDate", matchCase = false, @operator = "notequal", predicate = "and", value = (DateTime?)null }); 
} 
 
 
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowFiltering(true).Columns(col => 
{ 
   col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
   col.Field("EmployeeID").HeaderText("Employee ID").Width("150").Add(); 
   col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add(); 
   col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
   col.Field("ShippedDate").HeaderText("Shipped Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add(); 
 
}).FilterSettings(filter => filter.Columns(filterColumns)).Load("load").AllowPaging().Render() 
 
<script> 
    function load(args) { 
 
        this.filterSettings.columns.filter(function (e) { 
            debugger 
            if (!e.properties.value) { 
                e.properties.value = null; 
            } 
        }.bind(this)); 
         
 
    } 
</script> 
 
 
Regards, 
Thavasianand S.  



RW Richard Werning September 4, 2019 02:14 PM

Thank you, this solved the problem I was seeing. However this just seems like a work around to a bug in your source. Are you going to fix this issue or am I really going to need to override the value in load if it's undefined every place we want to do something like this?

Thank you,
Rich W

Loader.
Live Chat Icon For mobile
Up arrow icon