From 4514bc86b2ef25990c4fd6eed0f720beb08ffa06 Mon Sep 17 00:00:00 2001 From: Andrew Horth Date: Fri, 15 Sep 2023 14:05:56 +0100 Subject: [PATCH] Amended to show pages numbers where we know we have all possible search results returned --- .../Pages/Persons/Index.cshtml | 31 +++++++++---------- .../Pages/Persons/Index.cshtml.cs | 19 +++++++++++- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml index 166932c84a..52d35da1af 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml @@ -22,7 +22,6 @@ Search - Sort by @ContactSearchSortByOption.LastNameAscending.GetDisplayName() @ContactSearchSortByOption.LastNameDescending.GetDisplayName() @ContactSearchSortByOption.FirstNameAscending.GetDisplayName() @@ -38,21 +37,6 @@ {
- @if (Model.TotalKnownPages > 1) - { - - @if (Model.PreviousPage.HasValue) - { - - } - - @if (Model.NextPage.HasValue) - { - - } - - } - @@ -92,6 +76,21 @@ } + @if (Model.DisplayPageNumbers) + { + @for (int i = 0; i < Model.PaginationPages!.Length; i++) + { + var item = Model.PaginationPages[i]; + + if (i > 0 && Model.PaginationPages[i - 1] != item - 1) + { + + } + + @item + } + } + @if (Model.NextPage.HasValue) { diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml.cs index 7f996caa8d..50ea5d7832 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Pages/Persons/Index.cshtml.cs @@ -32,19 +32,24 @@ public IndexModel( public string? Search { get; set; } [BindProperty(SupportsGet = true)] + [Display(Name = "Sort by")] public ContactSearchSortByOption SortBy { get; set; } [BindProperty(SupportsGet = true)] public int? PageNumber { get; set; } - public PersonInfo[]? SearchResults { get; set; } + public int[]? PaginationPages { get; set; } public int TotalKnownPages { get; set; } + public bool DisplayPageNumbers { get; set; } + public int? PreviousPage { get; set; } public int? NextPage { get; set; } + public PersonInfo[]? SearchResults { get; set; } + public async Task OnGet() { PageNumber ??= 1; @@ -100,6 +105,18 @@ private async Task PerformSearch() PreviousPage = PageNumber > 1 ? PageNumber - 1 : null; NextPage = PageNumber < TotalKnownPages ? PageNumber + 1 : null; + if (contacts.Length < MaxSearchResultCount) + { + DisplayPageNumbers = true; + PaginationPages = Enumerable.Range(-2, 5).Select(offset => PageNumber!.Value + offset) + .Append(1) + .Append(TotalKnownPages) + .Where(page => page <= TotalKnownPages && page >= 1) + .Distinct() + .Order() + .ToArray(); + } + SearchResults = contacts .Skip((PageNumber!.Value - 1) * PageSize) .Take(PageSize)