diff --git a/LANCommander/Pages/Games/Index.razor b/LANCommander/Pages/Games/Index.razor index 6b558ea..cba53f4 100644 --- a/LANCommander/Pages/Games/Index.razor +++ b/LANCommander/Pages/Games/Index.razor @@ -4,6 +4,7 @@ @using System.ComponentModel.DataAnnotations; @using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; @using Microsoft.EntityFrameworkCore; +@using System.Web @attribute [Authorize] @inject GameService GameService @inject NavigationManager NavigationManager @@ -13,7 +14,7 @@ - + @@ -24,7 +25,7 @@ - +
@@ -121,27 +122,67 @@ bool Loading = true; string Search = ""; + string Url; bool Visibility = false; TableColumnPicker Picker; bool ColumnPickerVisible = false; - protected override async Task OnAfterRenderAsync(bool firstRender) + int PageIndex = 1; + int PageSize = 25; + + protected override async Task OnInitializedAsync() { - if (firstRender) - { - LoadData(); + Url = NavigationManager.Uri; + NavigationManager.LocationChanged += LocationChanged; + LoadTableParameter(); + await LoadData(); - Loading = false; - - StateHasChanged(); - } + Loading = false; } private async Task LoadData() { Games = await GameService.Get(g => g.Title.ToLower().Contains(Search.ToLower().Trim()) || g.SortTitle.ToLower().Contains(Search.ToLower().Trim())).OrderBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle).ToListAsync(); + + await InvokeAsync(StateHasChanged); + } + + private async void LocationChanged(object sender, LocationChangedEventArgs e) + { + Url = e.Location; + LoadTableParameter(); + await LoadData(); + } + + private void LoadTableParameter() + { + var uri = NavigationManager.ToAbsoluteUri(Url); + var query = HttpUtility.ParseQueryString(uri.Query); + + PageIndex = int.TryParse(query["Page"], out var index) ? index > 0 ? index : 1 : 1; + PageSize = int.TryParse(query["Size"], out var size) ? size > 0 ? size : 25 : 25; + + if (query["Search"] != null) + Search = query["Search"]; + else + Search = ""; + } + + private void PageIndexChanged(PaginationEventArgs args) + { + NavigationManager.NavigateTo($"Games?Page={args.Page}&Size={args.PageSize}{(Search != "" ? "&Search=" + Search : "")}"); + } + + private void PageSizeChanged(PaginationEventArgs args) + { + NavigationManager.NavigateTo($"Games?Page={args.Page}&Size={args.PageSize}{(Search != "" ? "&Search=" + Search : "")}"); + } + + private void SearchChanged() + { + NavigationManager.NavigateTo($"Games?Search={Search}"); } private string GetIcon(Game game) @@ -198,4 +239,9 @@ { ColumnPickerVisible = false; } + + public void Dispose() + { + NavigationManager.LocationChanged -= LocationChanged; + } }