Add search, page size, and page index to router

lx-work
Pat Hartl 2023-12-01 18:21:10 -06:00
parent a8c62151f3
commit 7568688c97
1 changed files with 56 additions and 10 deletions

View File

@ -4,6 +4,7 @@
@using System.ComponentModel.DataAnnotations; @using System.ComponentModel.DataAnnotations;
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; @using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@using Microsoft.EntityFrameworkCore; @using Microsoft.EntityFrameworkCore;
@using System.Web
@attribute [Authorize] @attribute [Authorize]
@inject GameService GameService @inject GameService GameService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@ -13,7 +14,7 @@
<PageHeaderExtra> <PageHeaderExtra>
<Space Direction="DirectionVHType.Horizontal"> <Space Direction="DirectionVHType.Horizontal">
<SpaceItem> <SpaceItem>
<Search Placeholder="Search" @bind-Value="Search" BindOnInput DebounceMilliseconds="150" OnChange="() => LoadData()" /> <Search Placeholder="Search" @bind-Value="Search" BindOnInput DebounceMilliseconds="250" OnChange="SearchChanged" />
</SpaceItem> </SpaceItem>
<SpaceItem> <SpaceItem>
<Button OnClick="() => Add()" Type="@ButtonType.Primary">Add Game</Button> <Button OnClick="() => Add()" Type="@ButtonType.Primary">Add Game</Button>
@ -24,7 +25,7 @@
<TableColumnPicker @ref="Picker" Key="Games" @bind-Visible="ColumnPickerVisible" /> <TableColumnPicker @ref="Picker" Key="Games" @bind-Visible="ColumnPickerVisible" />
<Table TItem="Game" DataSource="@Games" Loading="@Loading" PageSize="25" Responsive> <Table TItem="Game" DataSource="@Games" Loading="@Loading" PageSize="@PageSize" PageIndex="@PageIndex" OnPageIndexChange="PageIndexChanged" OnPageSizeChange="PageSizeChanged" Responsive>
<Column TData="string" Title="Icon" Hidden="@(Picker.IsColumnHidden("Icon"))"> <Column TData="string" Title="Icon" Hidden="@(Picker.IsColumnHidden("Icon"))">
<Image Src="@GetIcon(context)" Height="32" Width="32" Preview="false"></Image> <Image Src="@GetIcon(context)" Height="32" Width="32" Preview="false"></Image>
</Column> </Column>
@ -121,27 +122,67 @@
bool Loading = true; bool Loading = true;
string Search = ""; string Search = "";
string Url;
bool Visibility = false; bool Visibility = false;
TableColumnPicker Picker; TableColumnPicker Picker;
bool ColumnPickerVisible = false; bool ColumnPickerVisible = false;
protected override async Task OnAfterRenderAsync(bool firstRender) int PageIndex = 1;
int PageSize = 25;
protected override async Task OnInitializedAsync()
{ {
if (firstRender) Url = NavigationManager.Uri;
{ NavigationManager.LocationChanged += LocationChanged;
LoadData(); LoadTableParameter();
await LoadData();
Loading = false; Loading = false;
StateHasChanged();
}
} }
private async Task LoadData() 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(); 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) private string GetIcon(Game game)
@ -198,4 +239,9 @@
{ {
ColumnPickerVisible = false; ColumnPickerVisible = false;
} }
public void Dispose()
{
NavigationManager.LocationChanged -= LocationChanged;
}
} }