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();
{
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?