Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<TGridItem>(
startIndex, Pagination?.ItemsPerPage, _sortByColumn, _sortByAscending, thisLoadCts.Token);
Expand All @@ -352,6 +351,7 @@ private async Task RefreshDataCoreAsync()
Pagination?.SetTotalItemCountAsync(result.TotalItemCount);
_pendingDataLoadCancellationTokenSource = null;
}
_lastRefreshedPaginationStateHash = Pagination?.GetHashCode();
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Components/test/E2ETest/Tests/QuickGridTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuickGridFilterComponent>();

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<QuickGridFilterComponent>();

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()
{
Expand Down
1 change: 1 addition & 0 deletions src/Components/test/testassets/BasicTestApp/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<option value="@GetTestServerProjectComponent("Components.TestServer.ProtectedBrowserStorageInjectionComponent")">Protected browser storage injection</option>
<option value="BasicTestApp.QuickGridTest.SampleQuickGridComponent">QuickGrid Example</option>
<option value="BasicTestApp.QuickGridTest.QuickGridVirtualizeComponent">QuickGrid with Virtualize Example</option>
<option value="BasicTestApp.QuickGridTest.QuickGridFilterComponent">QuickGrid Filter ItemsProvider Calls Test</option>
<option value="BasicTestApp.RazorTemplates">Razor Templates</option>
<option value="BasicTestApp.Reconnection.ReconnectionComponent">Reconnection server-side blazor</option>
<option value="BasicTestApp.RedTextComponent">Red text</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@using Microsoft.AspNetCore.Components.QuickGrid

<div>
<input id="filter-input" type="text" @bind="filter" />
<button id="apply-filter-reset-pagination-btn" @onclick="ApplyFilterResetPagination">Apply Filter</button>
<button id="apply-filter-refresh-data-btn" @onclick="ApplyFilterRefreshData">Apply Filter</button>
</div>

<div class="grid">
<QuickGrid ItemsProvider="itemsProvider" Pagination="@pagination" @ref="gridRef">
<PropertyColumn Property="@(c => c.Id)" />
<PropertyColumn Property="@(c => c.Name)" />
</QuickGrid>
</div>
<Paginator State="@pagination" />

<p id="items-provider-calls">@itemsProviderCalls</p>

@code {
PaginationState pagination = new PaginationState { ItemsPerPage = 5 };
int itemsProviderCalls = 0;
GridItemsProvider<Item> itemsProvider;
QuickGrid<Item> gridRef;
string filter = string.Empty;

private readonly List<Item> 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;

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;
}
}
Loading