Search code examples
asp.net-mvcasp.net-corekendo-gridkendo-asp.net-mvc

Filter grid by date range


I'm currently using ASP.NET Core Kendo Grid as:

@(Html.Kendo().Grid(Model)
                    .Name("grid")
                    .ToolBar(t =>
                    {
                        t.Search();
                        t.Custom().Name("Clear").IconClass("mdi mdi-refresh");
                    })
                    .DataSource(dataSource => dataSource
                        .Custom()
                        .PageSize(10)
                    )
                    .Pageable(pager => pager
                        .Position(GridPagerPosition.Bottom)
                    )
                    .Sortable()
                    .Events(events => events.DataBound("onDataBound")
                    )
                    .Columns(columns =>
                    {
                      ...
                        columns.Bound(x => x.StartDate)
                            .Format("{0:MM/dd/yyyy}")
                            .Title("From Date");
                        columns.Bound(x => x.EndDate)
                            .Format("{0:MM/dd/yyyy}")
                            .Title("To Date");
                    })
                    )

As you can see I have a StartDate and EndDate columns, I want to add a DateRange calendar filter, if the date is between those dates, filter the table. How can I achieve that with .Net Core Grid? Regards


Solution

  • This way you are telling Kendo to just put start and end date range for each Date type column filter menu and remove extra items:

     @(Html.Kendo().Grid(Model)
             .Name("grid")
             .Filterable(filterable => filterable.Operators(operators =>
              {
                  operators.ForDate(options =>
                  {
                      options.Clear();
                      options.IsGreaterThanOrEqualTo("From Date");
                      options.IsLessThanOrEqualTo("To Date");
                  });
              }))
             .Columns(columns =>
              {
               ....
                columns.Bound(x => x.StartDate)
                            .Format("{0:MM/dd/yyyy}")
                            .Title("From Date");
                        columns.Bound(x => x.EndDate)
                            .Format("{0:MM/dd/yyyy}")
                            .Title("To Date");
             })
        )