Skip to content

Commit

Permalink
Merge pull request #566 from irihitech/page
Browse files Browse the repository at this point in the history
Fix pagination initialization issue.
  • Loading branch information
rabbitism authored Feb 27, 2025
2 parents c2661a5 + 021225d commit 55031c5
Show file tree
Hide file tree
Showing 3 changed files with 448 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/Ursa.Demo/Pages/PaginationDemo.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<u:Pagination
Name="page"
PageSizeOptions="10, 20, 50, 100"
CurrentPage="5"
ShowQuickJump="{Binding #quickJumperSelector.IsChecked}"
ShowPageSizeSelector="{Binding #pageSizeSelector.IsChecked}"
Command="{Binding LoadPageCommand}"
Expand Down
19 changes: 12 additions & 7 deletions src/Ursa/Controls/Pagination/Pagination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ public bool DisplayCurrentPageInQuickJumper
private static int? CoerceCurrentPage(AvaloniaObject arg1, int? arg2)
{
if (arg2 is null) return null;
if (arg1 is Pagination p) arg2 = MathHelpers.SafeClamp(arg2.Value, 1, p.PageCount);
// Only coerce the value if the pagination is initialized. Otherwise the value will be coerced to default because PageCount is not yet determined.
if (arg1 is Pagination { IsInitialized: true } p)
{
arg2 = MathHelpers.SafeClamp(arg2.Value, 1, p.PageCount);
}
return arg2;
}

Expand All @@ -193,6 +197,7 @@ public event EventHandler<ValueChangedEventArgs<int>>? CurrentPageChanged

private void OnPageSizeChanged(AvaloniaPropertyChangedEventArgs<int> args)
{
if (!IsInitialized) return;
var pageCount = TotalCount / args.NewValue.Value;
var residue = TotalCount % args.NewValue.Value;
if (residue > 0) pageCount++;
Expand All @@ -218,7 +223,8 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
LostFocusEvent.AddHandler(OnQuickJumpInputLostFocus, _quickJumpInput);

InitializePanelButtons();
UpdateButtonsByCurrentPage(0);
CurrentPage = MathHelpers.SafeClamp(CurrentPage ?? 1, 1, PageCount);
UpdateButtonsByCurrentPage(CurrentPage);
}

private void OnQuickJumpInputKeyDown(object? sender, KeyEventArgs e)
Expand Down Expand Up @@ -300,16 +306,15 @@ private void UpdateButtonsByCurrentPage(int? page)
{
if (PageSize == 0) return;
var pageCount = TotalCount / PageSize;
var currentPage = CurrentPage;
var residue = TotalCount % PageSize;
if (residue > 0) pageCount++;
if (_buttonPanel is null)
{
SetCurrentValue(PageCountProperty, pageCount);
return;
}

var currentPage = CurrentPage;
var residue = TotalCount % PageSize;
if (residue > 0) pageCount++;


if (pageCount <= 7)
{
for (var i = 0; i < 7; i++)
Expand Down
Loading

0 comments on commit 55031c5

Please sign in to comment.