Apply filter to grid on page load

1 Answer 163 Views
Filter Grid
Bermuda
Top achievements
Rank 1
Iron
Bermuda asked on 18 Jul 2023, 07:37 PM

Hello,

I have an Ajax Kendo grid and would like to apply a filter on load.  There is a date column that I would like to get the max date and use that as the initial filter (not hardcoding the value).  Is this doable and if so how?

Thanks

D

1 Answer, 1 is accepted

Sort by
0
Accepted
Mihaela
Telerik team
answered on 21 Jul 2023, 03:30 PM

Hello,

Since you have submitted the same query as a support ticket, and I have already answered your question in the support thread, I will post the same here in case someone else has the same question:

The easiest approach will be to get the latest date on the server, store it in ViewData in the Action method that returns the View with the Grid, and pass it in the Filter configuration. For example:

  • Action method that returns the Grid View:
public IActionResult Index()
{
  //access the Grid data from the sort the data to get the latest date
  ViewData["maxDate"] = new DateTime(2022, 1, 15); //store the date in ViewData
  return View();
}
.DataSource(dataSource => dataSource
                .Ajax()
                ...
                .Filter(filters =>
                {
                    filters.Add(product => product.OrderDate).IsGreaterThanOrEqualTo(ViewData["maxDate"] as DateTime?);
                })
)

 

Regards, Mihaela Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Filter Grid
Asked by
Bermuda
Top achievements
Rank 1
Iron
Answers by
Mihaela
Telerik team
Share this question
or