2021-10-31 14:23:03 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2020-07-10 17:57:20 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-02 19:15:15 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
public partial class Table<TItem> : ITable
|
2020-06-02 19:15:15 +08:00
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
2020-06-16 14:44:21 +08:00
|
|
|
|
public bool HidePagination { get; set; }
|
2020-06-02 19:15:15 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// topLeft | topCenter | topRight |bottomLeft | bottomCenter | bottomRight
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
2020-07-02 20:30:01 +08:00
|
|
|
|
public string PaginationPosition
|
|
|
|
|
{
|
|
|
|
|
get => _paginationPosition;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_paginationPosition = value;
|
2020-07-10 17:57:20 +08:00
|
|
|
|
InitializePagination();
|
2020-07-02 20:30:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-02 19:15:15 +08:00
|
|
|
|
|
2021-04-25 14:09:24 +08:00
|
|
|
|
[Parameter]
|
2022-01-04 13:49:46 +08:00
|
|
|
|
public RenderFragment<(int PageSize, int PageIndex, int Total, string PaginationClass, EventCallback<PaginationEventArgs> HandlePageChange)> PaginationTemplate { get; set; }
|
2021-04-25 14:09:24 +08:00
|
|
|
|
|
2020-06-02 19:15:15 +08:00
|
|
|
|
[Parameter]
|
2021-06-15 16:13:06 +08:00
|
|
|
|
public int Total { get; set; }
|
2020-06-02 19:15:15 +08:00
|
|
|
|
|
2020-06-03 00:22:10 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<int> TotalChanged { get; set; }
|
|
|
|
|
|
2020-06-02 19:15:15 +08:00
|
|
|
|
[Parameter]
|
2020-11-08 16:55:34 +08:00
|
|
|
|
public int PageIndex
|
|
|
|
|
{
|
|
|
|
|
get => _pageIndex;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_pageIndex != value)
|
|
|
|
|
{
|
|
|
|
|
_pageIndex = value;
|
|
|
|
|
_waitingReloadAndInvokeChange = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-28 12:51:36 +08:00
|
|
|
|
|
2020-06-03 00:22:10 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<int> PageIndexChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-11-08 16:55:34 +08:00
|
|
|
|
public int PageSize
|
|
|
|
|
{
|
|
|
|
|
get => _pageSize;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_pageSize != value)
|
|
|
|
|
{
|
|
|
|
|
_pageSize = value;
|
|
|
|
|
_waitingReloadAndInvokeChange = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-28 12:51:36 +08:00
|
|
|
|
|
2020-06-03 00:22:10 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<int> PageSizeChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<PaginationEventArgs> OnPageIndexChange { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<PaginationEventArgs> OnPageSizeChange { get; set; }
|
|
|
|
|
|
2021-02-28 12:51:36 +08:00
|
|
|
|
private int _total;
|
|
|
|
|
private int _dataSourceCount;
|
2020-07-02 20:30:01 +08:00
|
|
|
|
private string _paginationPosition = "bottomRight";
|
|
|
|
|
private string _paginationClass;
|
2020-11-08 16:55:34 +08:00
|
|
|
|
private int _pageIndex = 1;
|
|
|
|
|
private int _pageSize = 10;
|
2020-06-02 19:15:15 +08:00
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
private void InitializePagination()
|
2020-07-02 20:30:01 +08:00
|
|
|
|
{
|
|
|
|
|
_paginationClass = $"ant-table-pagination ant-table-pagination-{Regex.Replace(_paginationPosition, "bottom|top", "").ToLowerInvariant()}";
|
|
|
|
|
}
|
2020-06-03 00:22:10 +08:00
|
|
|
|
|
2021-03-11 23:26:49 +08:00
|
|
|
|
private async Task HandlePageChange(PaginationEventArgs args)
|
|
|
|
|
{
|
2022-01-11 16:44:48 +08:00
|
|
|
|
bool shouldInvokeChange = false;
|
|
|
|
|
|
|
|
|
|
if (_pageSize != args.PageSize)
|
|
|
|
|
{
|
|
|
|
|
shouldInvokeChange = true;
|
|
|
|
|
await HandlePageSizeChange(args);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 23:26:49 +08:00
|
|
|
|
if (_pageIndex != args.Page)
|
|
|
|
|
{
|
2022-01-11 16:44:48 +08:00
|
|
|
|
shouldInvokeChange = true;
|
2021-03-11 23:26:49 +08:00
|
|
|
|
await HandlePageIndexChange(args);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 16:44:48 +08:00
|
|
|
|
if (shouldInvokeChange)
|
2021-03-11 23:26:49 +08:00
|
|
|
|
{
|
2022-01-11 16:44:48 +08:00
|
|
|
|
ReloadAndInvokeChange();
|
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
2021-03-11 23:26:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
private async Task HandlePageIndexChange(PaginationEventArgs args)
|
2020-06-03 00:22:10 +08:00
|
|
|
|
{
|
2021-03-11 23:26:49 +08:00
|
|
|
|
_pageIndex = args.Page;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2021-03-06 10:20:41 +08:00
|
|
|
|
if (PageIndexChanged.HasDelegate)
|
|
|
|
|
{
|
2021-03-11 23:26:49 +08:00
|
|
|
|
await PageIndexChanged.InvokeAsync(args.Page);
|
2021-03-06 10:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (OnPageIndexChange.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnPageIndexChange.InvokeAsync(args);
|
|
|
|
|
}
|
2020-06-03 00:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 23:26:49 +08:00
|
|
|
|
private async Task HandlePageSizeChange(PaginationEventArgs args)
|
2020-06-03 00:22:10 +08:00
|
|
|
|
{
|
2021-03-06 10:20:41 +08:00
|
|
|
|
_pageSize = args.PageSize;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2021-03-06 10:20:41 +08:00
|
|
|
|
if (PageSizeChanged.HasDelegate)
|
|
|
|
|
{
|
2021-03-11 23:26:49 +08:00
|
|
|
|
await PageSizeChanged.InvokeAsync(args.PageSize);
|
2021-03-06 10:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (OnPageSizeChange.HasDelegate)
|
|
|
|
|
{
|
2021-03-11 23:26:49 +08:00
|
|
|
|
await OnPageSizeChange.InvokeAsync(args);
|
2021-03-06 10:20:41 +08:00
|
|
|
|
}
|
2020-06-03 00:22:10 +08:00
|
|
|
|
}
|
2021-10-31 14:23:03 +08:00
|
|
|
|
|
|
|
|
|
private void ChangePageSize(int pageSize)
|
|
|
|
|
{
|
|
|
|
|
pageSize = Math.Max(1, pageSize);
|
|
|
|
|
|
|
|
|
|
if (_pageSize == pageSize)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pageSize = pageSize;
|
|
|
|
|
|
|
|
|
|
if (PageSizeChanged.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
PageSizeChanged.InvokeAsync(_pageSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ChangePageIndex(int pageIndex)
|
|
|
|
|
{
|
|
|
|
|
pageIndex = Math.Max(1, pageIndex);
|
|
|
|
|
|
|
|
|
|
if (_pageIndex == pageIndex)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pageIndex = pageIndex;
|
|
|
|
|
|
|
|
|
|
if (PageIndexChanged.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
PageIndexChanged.InvokeAsync(_pageIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-02 19:15:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|