Custom OnRead Handler

1 Answer 464 Views
Grid
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
Adrian asked on 06 Jul 2021, 04:56 PM

I have a Telerik Blazor Grid where I am doing a custom OnRead event to get the data.

I am trying to detect within that event handler when the grid filters have changed as I need to request extra data if someone changes the grid filters in anyway.

I have tried comparing the previous filters with the new request filters but it says they are always different even when just requesting a new page in the grid.

My code looks like this:

protected override async Task OnReadHandler(GridReadEventArgs e)
{
    if (!ReHydrateGridFromPersistedState)
    {
        //no grid state exists so call the database for data
        var filtersHaveChanged = GridRequest != null && !GridRequest.Filters.Equals(e.Request.Filters);

        GridRequest = e.Request;

        if (filtersHaveChanged)
        {
            ExtraData = await GetExtraDataAsync();
        }

        await base.OnReadHandler(e);
    }
    else
    {
        GridRequest = e.Request;
    }
}
var filtersHaveChanged = GridRequest != null && !GridRequest.Filters.Equals(e.Request.Filters);

/* filtersHaveChanged is ALWAYS true */

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 06 Jul 2021, 05:57 PM

Hi Adrian,

Generally speaking, you should request the new data every time OnRead fires, that is an indication that the user changed something in the grid that demands new data. Thus, fetching data only for some cases will result in your users seeing invalid information.

That said, I can suggest you consider the StateChanged event which tells which aspect of the grid was last modified through its event arguments (say, filter descriptors). You can read more about it in the Grid State article (see the "Get and Override User Action That Changes The Grid" section for a particular example about getting that info).

The third option would be to write a comparer for the filter descriptors that compares the actual values rather than references (references is how the framework compares objects by default).

Regards,
Marin Bratanov
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.

Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
commented on 07 Jul 2021, 07:25 AM

Thanks for the help. Do you have a list of all the possible string values that can be in GridStateEventArgs<TItem>.PropertyName ? Currently I only know of "Page" and "FilterDescriptors"
Marin Bratanov
Telerik team
commented on 07 Jul 2021, 06:56 PM

I made a pull request with that info for the docs, it will be live when it gets merged and in the meantime you can see the list in the PR.
Tags
Grid
Asked by
Adrian
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Marin Bratanov
Telerik team
Share this question
or