@namespace Oqtane.Modules.Controls
@inherits ModuleBase
@typeparam TableItem
@Header
@foreach (var item in ItemList)
{
@Row(item)
}
@if (Page > MaxPages)
{
}
@if (EndPage > 1)
{
@for (int i = StartPage; i <= EndPage; i++)
{
var pager = i;
}
}
@if (EndPage < Pages)
{
}
@if (EndPage > 1)
{
Page @Page of @Pages
}
@code {
int Pages;
int Page;
int MaxItems;
int MaxPages;
int StartPage;
int EndPage;
[Parameter]
public RenderFragment Header { get; set; }
[Parameter]
public RenderFragment Row { get; set; }
[Parameter]
public IEnumerable Items { get; set; }
[Parameter]
public string PageSize { get; set; }
[Parameter]
public string DisplayPages { get; set; }
IEnumerable ItemList { get; set; }
protected override void OnInitialized()
{
if (string.IsNullOrEmpty(PageSize))
{
MaxItems = 10;
}
else
{
MaxItems = int.Parse(PageSize);
}
if (string.IsNullOrEmpty(DisplayPages))
{
MaxPages = 5;
}
else
{
MaxPages = int.Parse(DisplayPages);
}
Page = 1;
ItemList = Items.Skip((Page - 1) * MaxItems).Take(MaxItems);
Pages = (int)Math.Ceiling(Items.Count() / (decimal)MaxItems);
SetPagerSize("forward");
}
public void UpdateList(int CurrentPage)
{
ItemList = Items.Skip((Page - 1) * MaxItems).Take(MaxItems);
Page = CurrentPage;
StateHasChanged();
}
public void SetPagerSize(string direction)
{
if (direction == "forward")
{
StartPage = EndPage + 1;
if (EndPage + MaxPages < Pages)
{
EndPage = StartPage + MaxPages - 1;
}
else
{
EndPage = Pages;
}
StateHasChanged();
}
else if (direction == "back")
{
EndPage = StartPage - 1;
StartPage = StartPage - MaxPages;
}
}
public void NavigateToPage(string direction)
{
if (direction == "next")
{
if (Page < Pages)
{
if (Page == EndPage)
{
SetPagerSize("forward");
}
Page += 1;
}
}
else if (direction == "previous")
{
if (Page > 1)
{
if (Page == StartPage)
{
SetPagerSize("back");
}
Page -= 1;
}
}
UpdateList(Page);
}
}