Skip to content

Commit f95786c

Browse files
committed
Feedback
1 parent 683b986 commit f95786c

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -314,38 +314,40 @@ public async Task RefreshDataAsync()
314314
private async Task RefreshDataCoreAsync()
315315
{
316316
// First render of Virtualize component will handle the data load itself.
317-
if (!(_firstRefreshDataAsync && Virtualize))
317+
if (_firstRefreshDataAsync && Virtualize)
318318
{
319-
// Move into a "loading" state, cancelling any earlier-but-still-pending load
320-
_pendingDataLoadCancellationTokenSource?.Cancel();
321-
var thisLoadCts = _pendingDataLoadCancellationTokenSource = new CancellationTokenSource();
319+
_firstRefreshDataAsync = false;
320+
return;
321+
}
322+
323+
// Move into a "loading" state, cancelling any earlier-but-still-pending load
324+
_pendingDataLoadCancellationTokenSource?.Cancel();
325+
var thisLoadCts = _pendingDataLoadCancellationTokenSource = new CancellationTokenSource();
322326

323-
if (_virtualizeComponent is not null)
327+
if (_virtualizeComponent is not null)
328+
{
329+
// If we're using Virtualize, we have to go through its RefreshDataAsync API otherwise:
330+
// (1) It won't know to update its own internal state if the provider output has changed
331+
// (2) We won't know what slice of data to query for
332+
await _virtualizeComponent.RefreshDataAsync();
333+
_pendingDataLoadCancellationTokenSource = null;
334+
}
335+
else
336+
{
337+
// If we're not using Virtualize, we build and execute a request against the items provider directly
338+
_lastRefreshedPaginationStateHash = Pagination?.GetHashCode();
339+
var startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage);
340+
var request = new GridItemsProviderRequest<TGridItem>(
341+
startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token);
342+
var result = await ResolveItemsRequestAsync(request);
343+
if (!thisLoadCts.IsCancellationRequested)
324344
{
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();
345+
_currentNonVirtualizedViewItems = result.Items;
346+
_ariaBodyRowCount = _currentNonVirtualizedViewItems.Count;
347+
Pagination?.SetTotalItemCountAsync(result.TotalItemCount);
329348
_pendingDataLoadCancellationTokenSource = null;
330349
}
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-
}
347350
}
348-
_firstRefreshDataAsync = false;
349351
}
350352

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

src/Components/test/E2ETest/Tests/QuickGridTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void RowStyleApplied()
166166
const p = document.querySelector('tbody > tr:first-child > td:nth-child(5)');
167167
return p ? getComputedStyle(p).textAlign : null;"));
168168
}
169-
169+
170170
[Fact]
171171
public void CanOpenColumnOptions()
172172
{
@@ -208,4 +208,11 @@ public void CanCloseColumnOptionsByHideColumnOptionsAsync()
208208
var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
209209
Browser.DoesNotExist(By.CssSelector(firstNameSearchSelector));
210210
}
211+
212+
[Fact]
213+
public void ItemsProviderCalledOnceWithVirtualize()
214+
{
215+
app = Browser.MountTestComponent<QuickGridVirtualizeComponent>();
216+
Browser.Equal("1", () => app.FindElement(By.Id("items-provider-call-count")).Text);
217+
}
211218
}

src/Components/test/testassets/BasicTestApp/Index.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<option value="@GetTestServerProjectComponent("Components.TestServer.ProtectedBrowserStorageUsageComponent")">Protected browser storage usage</option>
9595
<option value="@GetTestServerProjectComponent("Components.TestServer.ProtectedBrowserStorageInjectionComponent")">Protected browser storage injection</option>
9696
<option value="BasicTestApp.QuickGridTest.SampleQuickGridComponent">QuickGrid Example</option>
97+
<option value="BasicTestApp.QuickGridTest.QuickGridVirtualizeComponent">QuickGrid with Virtualize Example</option>
9798
<option value="BasicTestApp.RazorTemplates">Razor Templates</option>
9899
<option value="BasicTestApp.Reconnection.ReconnectionComponent">Reconnection server-side blazor</option>
99100
<option value="BasicTestApp.RedTextComponent">Red text</option>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@using Microsoft.AspNetCore.Components.QuickGrid
2+
@using System.Linq
3+
4+
<p id="items-provider-call-count">@ItemsProviderCallCount</p>
5+
6+
<div id="grid" style="height: 200px; overflow: auto">
7+
<QuickGrid ItemsProvider="@itemsProvider" Virtualize="true" ItemSize="50">
8+
<PropertyColumn Property="@(p => p.Id)" />
9+
<PropertyColumn Property="@(p => p.Name)" />
10+
</QuickGrid>
11+
</div>
12+
13+
@code {
14+
internal class Person
15+
{
16+
public int Id { get; set; }
17+
public string Name { get; set; } = string.Empty;
18+
}
19+
20+
private GridItemsProvider<Person> itemsProvider = default!;
21+
22+
int ItemsProviderCallCount = 0;
23+
24+
protected override void OnInitialized()
25+
{
26+
itemsProvider = async request =>
27+
{
28+
await Task.CompletedTask;
29+
Interlocked.Increment(ref ItemsProviderCallCount);
30+
StateHasChanged();
31+
return GridItemsProviderResult.From(
32+
items: Enumerable.Range(1, 100).Select(i => new Person { Id = i, Name = $"Person {i}" }).ToList(),
33+
totalItemCount: 100);
34+
};
35+
}
36+
}

0 commit comments

Comments
 (0)