Skip to content

Commit 0b833b0

Browse files
authored
Fix two loads in QuickGrid with Virtualize (#63616)
1 parent c0e5a4e commit 0b833b0

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

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

Lines changed: 9 additions & 0 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,6 +313,13 @@ public async Task RefreshDataAsync()
311313
// because in that case there's going to be a re-render anyway.
312314
private async Task RefreshDataCoreAsync()
313315
{
316+
// First render of Virtualize component will handle the data load itself.
317+
if (_firstRefreshDataAsync && Virtualize)
318+
{
319+
_firstRefreshDataAsync = false;
320+
return;
321+
}
322+
314323
// Move into a "loading" state, cancelling any earlier-but-still-pending load
315324
_pendingDataLoadCancellationTokenSource?.Cancel();
316325
var thisLoadCts = _pendingDataLoadCancellationTokenSource = new CancellationTokenSource();

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)