Server side pagination using grid and blazor

1 Answer 221 Views
Miscellaneous
n/a
Top achievements
Rank 1
Iron
Iron
Iron
n/a asked on 20 Dec 2023, 05:22 PM

Hello,

I would like to apply server-side pagination and display data in a grid. I am using Blazor and data is being fetched via an api request.

Already implemented:

Each api request brings a "requestResponse" which will contain "totalPages" and the "data" (the records). The "totalPages" is calculated on the server side based on the total number of records and page size. The page size is always 50 in this case. So e.g. if there are 400 records in a collection, the request will fetch: "totalPages" = 8, data = 50 records.

What I want to achieve: 

The final result should be a grid which is pageable. The grid should display number of pages equal to the "totalPages" from the api response. Each click on a page should trigger another api request (ideally fire an event that calls a method that takes as an argument the page number).

I saw documentation about OnRead event: https://docs.telerik.com/blazor-ui/components/grid/manual-operations but I couldn't make it work as when the component is initialized, the event doesn't fire. I cannot fire it within OnInitializedAsync() as I would need to pass GridReadEventArgs to the event and that is not possible in OnInitializedAsync() when the component is first initialized. If I'm not using GridReadEventArgs  as argument, then how would the grid know the "totalPages"?

Am I going in the right direction with OnRead? Is there another way, perhaps trigger the api within a PageChanged event? Then how would I be able to set the desired number of pages?

Any help would be appreciated.

Thank you.

Kind regards,

Astig

1 Answer, 1 is accepted

Sort by
0
Radko
Telerik team
answered on 25 Dec 2023, 09:49 AM

Hi Astig,

What you are after is indeed possible and the OnRead event is the right tool for the job. In essence, what it does is that it fires whenever the Grid needs a new set of data - e.g. when the Grid pages or loads for the first time. We have a few demo apps that demonstrate just that, which can be found here: https://github.com/telerik/blazor-ui/tree/master/grid/datasourcerequest-on-server (note some require setting multiple startup projects within VS, if this is the IDE you are using).

Please have a look and let me know if any more questions.

Regards,
Radko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
n/a
Top achievements
Rank 1
Iron
Iron
Iron
commented on 11 Jan 2024, 12:17 PM

Thank you very much Radko.

Kind regards,

Astig

Tags
Miscellaneous
Asked by
n/a
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Radko
Telerik team
Share this question
or