Skip to content

Commit eecc193

Browse files
committed
Fix two loads in QuickGrid with Virtualize
1 parent 0e1065a commit eecc193

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public partial class QuickGrid<TGridItem> : IAsyncDisposable
155155
// If the QuickGrid is disposed while the JS module is being loaded, we need to avoid calling JS methods
156156
private bool _wasDisposed;
157157

158+
private bool _firstRefreshDataAsync = true;
159+
158160
/// <summary>
159161
/// Constructs an instance of <see cref="QuickGrid{TGridItem}"/>.
160162
/// </summary>
@@ -311,34 +313,39 @@ public async Task RefreshDataAsync()
311313
// because in that case there's going to be a re-render anyway.
312314
private async Task RefreshDataCoreAsync()
313315
{
314-
// Move into a "loading" state, cancelling any earlier-but-still-pending load
315-
_pendingDataLoadCancellationTokenSource?.Cancel();
316-
var thisLoadCts = _pendingDataLoadCancellationTokenSource = new CancellationTokenSource();
317-
318-
if (_virtualizeComponent is not null)
319-
{
320-
// If we're using Virtualize, we have to go through its RefreshDataAsync API otherwise:
321-
// (1) It won't know to update its own internal state if the provider output has changed
322-
// (2) We won't know what slice of data to query for
323-
await _virtualizeComponent.RefreshDataAsync();
324-
_pendingDataLoadCancellationTokenSource = null;
325-
}
326-
else
316+
// First render of Virtualize component will handle the data load itself.
317+
if (!_firstRefreshDataAsync || !Virtualize)
327318
{
328-
// If we're not using Virtualize, we build and execute a request against the items provider directly
329-
_lastRefreshedPaginationStateHash = Pagination?.GetHashCode();
330-
var startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage);
331-
var request = new GridItemsProviderRequest<TGridItem>(
332-
startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token);
333-
var result = await ResolveItemsRequestAsync(request);
334-
if (!thisLoadCts.IsCancellationRequested)
319+
// Move into a "loading" state, cancelling any earlier-but-still-pending load
320+
_pendingDataLoadCancellationTokenSource?.Cancel();
321+
var thisLoadCts = _pendingDataLoadCancellationTokenSource = new CancellationTokenSource();
322+
323+
if (_virtualizeComponent is not null)
335324
{
336-
_currentNonVirtualizedViewItems = result.Items;
337-
_ariaBodyRowCount = _currentNonVirtualizedViewItems.Count;
338-
Pagination?.SetTotalItemCountAsync(result.TotalItemCount);
325+
// If we're using Virtualize, we have to go through its RefreshDataAsync API otherwise:
326+
// (1) It won't know to update its own internal state if the provider output has changed
327+
// (2) We won't know what slice of data to query for
328+
await _virtualizeComponent.RefreshDataAsync();
339329
_pendingDataLoadCancellationTokenSource = null;
340330
}
331+
else
332+
{
333+
// If we're not using Virtualize, we build and execute a request against the items provider directly
334+
_lastRefreshedPaginationStateHash = Pagination?.GetHashCode();
335+
var startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage);
336+
var request = new GridItemsProviderRequest<TGridItem>(
337+
startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token);
338+
var result = await ResolveItemsRequestAsync(request);
339+
if (!thisLoadCts.IsCancellationRequested)
340+
{
341+
_currentNonVirtualizedViewItems = result.Items;
342+
_ariaBodyRowCount = _currentNonVirtualizedViewItems.Count;
343+
Pagination?.SetTotalItemCountAsync(result.TotalItemCount);
344+
_pendingDataLoadCancellationTokenSource = null;
345+
}
346+
}
341347
}
348+
_firstRefreshDataAsync = false;
342349
}
343350

344351
// Gets called both by RefreshDataCoreAsync and directly by the Virtualize child component during scrolling

0 commit comments

Comments
 (0)