Custom data-source attribute for date filter

1 Answer 434 Views
Grid
JG
Top achievements
Rank 2
Iron
JG asked on 24 Jun 2021, 03:07 PM

I'm looking for a similar solution to the following for the grid filtering - 

http://www.crowbarsolutions.com/ignoring-time-when-filtering-dates-in-telerik-kendo-grids/

Currently the date filtering isn't working as it is obviously taking in to account the time element - which we don't want. Previously we followed the above article which worked great for our old mvc framework site, however now we are migrating to core it isn't possible to use the methods / overrides mentioned in the article.

 

Thanks

1 Answer, 1 is accepted

Sort by
1
Accepted
Aleksandar
Telerik team
answered on 29 Jun 2021, 07:01 AM

Hi Jack,

If you need to use only the date component of a DateTime instance you can use the Date property and pass only the date to the view and the Grid. This way the the time value will be set to 12:00:00 midnight (00:00:00) and filtering would work as expected as seen in this screencast. Here is a sample configuration:

Controller:

public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
{
    var result = Enumerable.Range(0, 50).Select(i => new OrderViewModel
    {
        OrderID = i,
        Freight = i * 10,
        OrderDate = DateTime.Now.AddDays(i % 7).Date,
        ShipName = "ShipName " + i,
        ShipCity = "ShipCity " + i
    });

    var dsResult = result.ToDataSourceResult(request);
    return Json(dsResult);
}

 

Grid:

@(Html.Kendo().Grid <OrderViewModel>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.OrderID).Filterable(false);
                columns.Bound(p => p.Freight);
                columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy hh:mm}"); // you can set a desired format date format here
                columns.Bound(p => p.ShipName).Width(500);
                columns.Bound(p => p.ShipCity);
                columns.Command(c => c.Edit()).Width(150);
            })
            .Pageable()
            .Editable(e=>e.Mode(GridEditMode.InLine))
            .Sortable()
            .Scrollable()
            .Filterable(f=>f.Mode(GridFilterMode.Row))
            .HtmlAttributes(new { style = "height:550px;" })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(20)
                .Read(read => read.Action("Orders_Read", "Grid"))
                )
        )

 

I hope this helps.

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

JG
Top achievements
Rank 2
Iron
commented on 30 Jun 2021, 12:56 PM

This worked perfectly thank you.

Just a little side note - as we were using LINQ to entities we had to use 'DbFunctions.TruncateTime' when setting up the iqueryable as .Date isn't supported for LINQ to entities.
Tags
Grid
Asked by
JG
Top achievements
Rank 2
Iron
Answers by
Aleksandar
Telerik team
Share this question
or