Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Environment. asp.net core, .net 8, Blazor
I have discovered a problem with the current implementation of the Pagination State control. The 2 methods that I pasted below have a problem.
When the user pages through existing data set (ex. user goes to page 5) and then executes a new query (with different criteria) that causes the total items count to be reduced and therefore, the total pages count to be reduced below the currently viewed page they are on (ex user is on page 5, but now we have only 2 pages), the pagination state control is coded to capture first the current total count and then reset the page count to LastPageIndex which causes another QuickGrid to run its query again.
When QuickGrid fetches the same data again (it fetches the same data twice, once do to criteria change, once because Pagination state forced it to by raising CurrentPageItemsChanged event), the total item count is the same as from previous query and therefore SetTotalItemCountAsync() method's first if statement will make the method exit which will never execute logic for raising the TotalItemCountChanged event which forces the paginators on the page to re-render.
`public Task SetCurrentPageIndexAsync(int pageIndex)
{
CurrentPageIndex = pageIndex;
return CurrentPageItemsChanged.InvokeCallbacksAsync(this);
}
internal Task SetTotalItemCountAsync(int totalItemCount)
{
if (totalItemCount == TotalItemCount)
{
return Task.CompletedTask;
}
TotalItemCount = totalItemCount;
if (CurrentPageIndex > 0 && CurrentPageIndex > LastPageIndex)
{
return SetCurrentPageIndexAsync(LastPageIndex.Value);
}
else
{
TotalItemCountChanged?.Invoke(this, TotalItemCount);
return TotalItemCountChangedSubscribable.InvokeCallbacksAsync(this);
}
}`
My code that forces refresh of data based on criteria changed.
public async Task RefreshDataAsync() { // after we changed the criteria, we tell quick grid to re-fetch using items ItemsProvider callback await quickGrid.RefreshDataAsync(); }
Expected Behavior
The pagers should render correct page count always, if the QuickGrid component fetches new data.
Quick HACK that fixes the problem by the use of a flag:
`
private bool _hasPendingTotalItemCountChangedEvent;
internal Task SetTotalItemCountAsync(int totalItemCount)
{
if (totalItemCount == TotalItemCount)
{
if (_hasPendingTotalItemCountChangedEvent)
{
return RaiseTotalItemCountChangedAsync();
}
return Task.CompletedTask;
}
TotalItemCount = totalItemCount;
if (CurrentPageIndex > 0 && CurrentPageIndex > LastPageIndex)
{
_hasPendingTotalItemCountChangedEvent = true; // remember to notify clients of TotalItemCountChanged
// If the number of items has reduced such that the current page index is no longer valid, move
// automatically to the final valid page index and trigger a further data load.
return SetCurrentPageIndexAsync(LastPageIndex.Value);
}
else
{
return RaiseTotalItemCountChangedAsync();
}
}
private Task RaiseTotalItemCountChangedAsync()
{
_hasPendingTotalItemCountChangedEvent = false; // clear the flag
// Under normal circumstances, we just want any associated pagination UI to update
TotalItemCountChanged?.Invoke(this, TotalItemCount);
return TotalItemCountChangedSubscribable.InvokeCallbacksAsync(this);
}`
Steps To Reproduce
See original description of the problem.
Exceptions (if any)
No response
.NET Version
.net 8
Anything else?
No response