-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #795 from EnchantedCoder/feature/issue-96
[HxGrid] Add support for custom pagination display providers
- Loading branch information
Showing
7 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Havit.Blazor.Components.Web.Bootstrap/Grids/GridPaginationTemplateContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace Havit.Blazor.Components.Web.Bootstrap; | ||
|
||
/// <summary> | ||
/// Context for the <see cref="HxGrid{TItem}.PaginationTemplate" /> of the <see cref="HxGrid{TItem}"/>. | ||
/// </summary> | ||
public sealed record GridPaginationTemplateContext | ||
{ | ||
private readonly Func<int, Task> _changeCurrentPageIndexAsyncFunc; | ||
|
||
internal GridPaginationTemplateContext(Func<int, Task> changeCurrentPageIndexAsyncFunc) | ||
{ | ||
_changeCurrentPageIndexAsyncFunc = changeCurrentPageIndexAsyncFunc; | ||
} | ||
|
||
/// <summary> | ||
/// Current grid user state (contains <see cref="GridUserState.PageIndex"/>). | ||
/// </summary> | ||
public GridUserState CurrentUserState { get; init; } | ||
|
||
/// <summary> | ||
/// Items per page. | ||
/// </summary> | ||
public int PageSize { get; init; } | ||
|
||
/// <summary> | ||
/// Total number of data items. | ||
/// </summary> | ||
public int TotalCount { get; init; } | ||
|
||
/// <summary> | ||
/// Settings for the pager (derived from <c>HxGrid.PagerSettings</c> .. <c>HxGrid.Settings.PagerSettings</c> .. <c>HxGrid.Defaults.PagerSettings</c> cascade). | ||
/// </summary> | ||
public PagerSettings PagerSettings { get; init; } | ||
|
||
/// <summary> | ||
/// Instructs the grid to change the current page index. | ||
/// </summary> | ||
public Task ChangeCurrentPageIndexAsync(int pageIndex) => _changeCurrentPageIndexAsyncFunc.Invoke(pageIndex); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Havit.Blazor.Documentation/Pages/Components/HxGridDoc/HxGrid_Demo_CustomPagination.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@inject IDemoDataService DemoDataService | ||
|
||
<HxGrid TItem="EmployeeDto" DataProvider="GetGridData" PageSize="_pageSize" Responsive="true"> | ||
<Columns> | ||
<HxGridColumn HeaderText="Name" ItemTextSelector="employee => employee.Name" SortKeySelector=" employee => employee.Name" IsDefaultSortColumn="true" /> | ||
<HxGridColumn HeaderText="Phone" ItemTextSelector="employee => employee.Phone" SortKeySelector=" employee => employee.Phone" /> | ||
<HxGridColumn HeaderText="Salary" ItemTextSelector="@(employee => employee.Salary.ToString("c0"))" SortKeySelector="employee => employee.Salary" /> | ||
<HxGridColumn HeaderText="Position" ItemTextSelector="employee => employee.Position" SortKeySelector="employee => employee.Position" /> | ||
<HxGridColumn HeaderText="Location" ItemTextSelector="employee => employee.Location" SortKeySelector="employee => employee.Location" /> | ||
</Columns> | ||
<PaginationTemplate Context="pagination"> | ||
@{ | ||
int totalPages = (pagination.TotalCount + pagination.PageSize - 1) / pagination.PageSize; | ||
int firstItemPosition = pagination.CurrentUserState.PageIndex * pagination.PageSize + 1; | ||
int lastItemPosition = Math.Min(firstItemPosition + pagination.PageSize - 1, pagination.TotalCount); | ||
} | ||
<div class="row"> | ||
<div class="col d-flex gap-2 align-items-center"> | ||
Rows per page: <HxSelect @bind-Value="@_pageSize" Data="_pageSizes" Nullable="false" AutoSort="false" InputSize="InputSize.Small" /> | ||
</div> | ||
<div class="col d-flex justify-content-center align-items-center"> | ||
Showing @firstItemPosition to @lastItemPosition of @pagination.TotalCount entries | ||
</div> | ||
<div class="col d-flex align-items-center justify-content-end"> | ||
<HxButton Icon="BootstrapIcon.ChevronBarLeft" Enabled="pagination.CurrentUserState.PageIndex > 0" OnClick="() => pagination.ChangeCurrentPageIndexAsync(0)" Color="ThemeColor.Link" /> | ||
<HxButton Icon="BootstrapIcon.ChevronLeft" Enabled="pagination.CurrentUserState.PageIndex > 0" OnClick="() => pagination.ChangeCurrentPageIndexAsync(pagination.CurrentUserState.PageIndex - 1)" Color="ThemeColor.Link" /> | ||
<HxButton Icon="BootstrapIcon.ChevronRight" Enabled="pagination.CurrentUserState.PageIndex + 1 < totalPages" OnClick="() => pagination.ChangeCurrentPageIndexAsync(pagination.CurrentUserState.PageIndex + 1)" Color="ThemeColor.Link" /> | ||
<HxButton Icon="BootstrapIcon.ChevronBarRight" Enabled="pagination.CurrentUserState.PageIndex + 1 < totalPages" OnClick="() => pagination.ChangeCurrentPageIndexAsync(totalPages - 1)" Color="ThemeColor.Link" /> | ||
</div> | ||
</div> | ||
</PaginationTemplate> | ||
</HxGrid> | ||
|
||
@code { | ||
private int _pageSize = 5; | ||
private readonly List<int> _pageSizes = [5, 10, 20]; | ||
|
||
private async Task<GridDataProviderResult<EmployeeDto>> GetGridData(GridDataProviderRequest<EmployeeDto> request) | ||
{ | ||
// you usually pass the data-request to your API/DataLayer | ||
return new GridDataProviderResult<EmployeeDto>() | ||
{ | ||
Data = await DemoDataService.GetEmployeesDataFragmentAsync(request.StartIndex, request.Count, request.CancellationToken), | ||
TotalCount = await DemoDataService.GetEmployeesCountAsync(request.CancellationToken) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters