Pagination not working with custom adapter
I have a DataGrid set up in a Blazor Webasembly project (PWA), but the pagination does not work after the first page.
It loads 20 records initially as expected, but when I go to page two, I get no records at all.
Here are the relevant parts of the SfGrid:
<GridPageSettings
PageSize="20"
PageSizes="new int[]{ 5, 10, 20, 50, 100 }">
</GridPageSettings>
<SfDataManager Adaptor="Adaptors.CustomAdaptor">
<OrphanCustomAdaptor></OrphanCustomAdaptor>
</SfDataManager>
As you can see, I've set the adaptor to be a component. I have tried it the other way with the same results.
Here are the relevant parts of the custom adapter:
@inherits DataAdaptor<IOrphanHttpRepository>
<CascadingValue Value="@this">
@ChildContent
</CascadingValue>
@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
public static List<Orphan> Orphans { get; set; }
bool gridInitialized = false;
public override async Task<object> ReadAsync(DataManagerRequest dm, string key = null)
{
if (!gridInitialized)
{
// Get data from API
Orphans = await Service.GetAllOrphansAsync();
gridInitialized = true;
}
IEnumerable<Orphan> DataSource = Orphans;
// search, sort, filter omitted.
int count = DataSource.Cast<Orphan>().Count();
if (dm.Skip != 0)
{
DataSource = DataSource.Skip(dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = DataSource.Take(dm.Take);
}
return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
With this setup, the first page loads fine but subsequent pages will not load any data.
I want to load all the records from the database via my API since there are only a few hundred. But I only want to do this once.
So I'm assigning the returned List to the Orphans field. And then using that to assign to DataSource. The data is there and the DataSource object does get the IEnumerable<Orphan> each time ReadAsync is called. But I only get records on the first page.
If I call await Service.GetAllOrphansAsync(); every time it works as expected, which makes no sense to me. And I don't want to get all the data from the API every single page. I only want to call that once.
This does work:
IEnumerable<Orphan> DataSource = await Service.GetAllOrphansAsync();
This does not work after the first page:
IEnumerable<Orphan> DataSource = Orphans;
(even though the data is there)
That method returns the same data that is in the Orphans field. But if I assign Orphans to DataSource, it doesn't work after page one, even though DataSource gets the same data.
This is baffling, to say the least. It's the same List!
Can you show me the correct way to do this?
SIGN IN To post a reply.
3 Replies
1 reply marked as answer
RN
Rahul Narayanasamy
Syncfusion Team
August 27, 2020 11:51 AM UTC
Hi David,
Greetings from Syncfusion.
Query: Pagination not working with custom adapter - With this setup, the first page loads fine but subsequent pages will not load any data.
We have validated your query and checked the reported problem based on the provided code snippets. You want to get the data from service at first time and you don’t want to get the data from service for subsequent times. In your code, you have tried to return Synchronous data from the Asynchronous method which is the cause of reported issue.
You can resolve the reported problem by using await Task.Yield(); in ReadAsync method. Find the below code snippets and sample for your reference.
|
public override async Task<object> ReadAsync(DataManagerRequest dm, string key = null)
{
if (!gridInitialized)
{
// Get data from API
Orphans = await Service.GetAllOrphansAsync();
gridInitialized = true;
}
await Task.Yield();
IEnumerable<Orphan> DataSource = Orphans;
// search, sort, filter omitted.
int count = DataSource.Cast<Orphan>().Count();
if (dm.Skip != 0)
{
DataSource = DataSource.Skip(dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = DataSource.Take(dm.Take);
}
return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
} |
Please let us know if you have any concerns.
Regards,
Rahul
Marked as answer
DE
David Evans
August 27, 2020 01:33 PM UTC
It works great. I had found another way, but I like this better. Thanks.
RN
Rahul Narayanasamy
Syncfusion Team
August 28, 2020 04:24 AM UTC
Hi David,
Thanks for the update. We are happy to hear that the provided solution resolved the problem.
Please get back to us if you need further assistance.
Regards,
Rahul
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
- Marked answer
-
DE David Evans
- Aug 21, 2020 08:19 PM UTC
- Aug 28, 2020 04:24 AM UTC