diff --git a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs index 88350bcfd207..f8d34dc34925 100644 --- a/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs +++ b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs @@ -340,7 +340,6 @@ private async Task RefreshDataCoreAsync() else { // If we're not using Virtualize, we build and execute a request against the items provider directly - _lastRefreshedPaginationStateHash = Pagination?.GetHashCode(); var startIndex = Pagination is null ? 0 : (Pagination.CurrentPageIndex * Pagination.ItemsPerPage); var request = new GridItemsProviderRequest( startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token); @@ -350,6 +349,7 @@ private async Task RefreshDataCoreAsync() _currentNonVirtualizedViewItems = result.Items; _ariaBodyRowCount = _currentNonVirtualizedViewItems.Count; Pagination?.SetTotalItemCountAsync(result.TotalItemCount); + _lastRefreshedPaginationStateHash = Pagination?.GetHashCode(); _pendingDataLoadCancellationTokenSource = null; } } diff --git a/src/Components/test/E2ETest/Tests/QuickGridTest.cs b/src/Components/test/E2ETest/Tests/QuickGridTest.cs index e2994a1eb41f..fcafb8c4ec1b 100644 --- a/src/Components/test/E2ETest/Tests/QuickGridTest.cs +++ b/src/Components/test/E2ETest/Tests/QuickGridTest.cs @@ -216,6 +216,36 @@ public void ItemsProviderCalledOnceWithVirtualize() Browser.Equal("1", () => app.FindElement(By.Id("items-provider-call-count")).Text); } + [Fact] + public void FilterUsingSetCurrentPageDoesNotCauseExtraRefresh() + { + app = Browser.MountTestComponent(); + + Browser.Equal("1", () => app.FindElement(By.Id("items-provider-calls")).Text); + + var filterInput = app.FindElement(By.Id("filter-input")); + filterInput.Clear(); + filterInput.SendKeys("Item 1"); + app.FindElement(By.Id("apply-filter-reset-pagination-btn")).Click(); + + Browser.Equal("2", () => app.FindElement(By.Id("items-provider-calls")).Text); + } + + [Fact] + public void FilterUsingRefreshDataDoesNotCauseExtraRefresh() + { + app = Browser.MountTestComponent(); + + Browser.Equal("1", () => app.FindElement(By.Id("items-provider-calls")).Text); + + var filterInput = app.FindElement(By.Id("filter-input")); + filterInput.Clear(); + filterInput.SendKeys("Item 1"); + app.FindElement(By.Id("apply-filter-refresh-data-btn")).Click(); + + Browser.Equal("2", () => app.FindElement(By.Id("items-provider-calls")).Text); + } + [Fact] public void OnRowClickTriggersCallback() { diff --git a/src/Components/test/testassets/BasicTestApp/Index.razor b/src/Components/test/testassets/BasicTestApp/Index.razor index 9cc2c8753b05..2ba106d7b87d 100644 --- a/src/Components/test/testassets/BasicTestApp/Index.razor +++ b/src/Components/test/testassets/BasicTestApp/Index.razor @@ -97,6 +97,7 @@ + diff --git a/src/Components/test/testassets/BasicTestApp/QuickGridTest/QuickGridFilterComponent.razor b/src/Components/test/testassets/BasicTestApp/QuickGridTest/QuickGridFilterComponent.razor new file mode 100644 index 000000000000..7b0414e4c2f1 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/QuickGridTest/QuickGridFilterComponent.razor @@ -0,0 +1,68 @@ +@using Microsoft.AspNetCore.Components.QuickGrid + +
+ + + +
+ +
+ + + + +
+ + +

@itemsProviderCalls

+ +@code { + PaginationState pagination = new PaginationState { ItemsPerPage = 5 }; + int itemsProviderCalls = 0; + GridItemsProvider itemsProvider = default!; + QuickGrid gridRef; + string filter = string.Empty; + + private readonly List allItems = Enumerable.Range(1, 25) + .Select(i => new Item { Id = i, Name = $"Item {i}" }) + .ToList(); + + protected override void OnInitialized() + { + itemsProvider = async request => + { + await Task.CompletedTask; + + Interlocked.Increment(ref itemsProviderCalls); + StateHasChanged(); + + var filteredItems = string.IsNullOrEmpty(filter) + ? allItems + : allItems.Where(i => i.Name.Contains(filter, StringComparison.OrdinalIgnoreCase)).ToList(); + + var totalCount = filteredItems.Count; + var pagedItems = filteredItems + .Skip(request.StartIndex) + .Take(request.Count ?? 5) + .ToList(); + + return GridItemsProviderResult.From(pagedItems, totalCount); + }; + } + + protected async Task ApplyFilterResetPagination() + { + await pagination.SetCurrentPageIndexAsync(0); + } + + protected async Task ApplyFilterRefreshData() + { + await gridRef.RefreshDataAsync(); + } + + class Item + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + } +}