using System.Diagnostics.CodeAnalysis;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
namespace Connected.Components;
public partial class TablePager : UIComponent
{
protected string Classname =>
new CssBuilder("mud-table-pagination-toolbar")
.AddClass("mud-tablepager-left", !RightToLeft)
.AddClass("mud-tablepager-right", RightToLeft)
.AddClass(Class)
.Build();
protected string PaginationClassname =>
new CssBuilder("mud-table-pagination-display")
.AddClass("mud-tablepager-left", !RightToLeft)
.AddClass("mud-tablepager-right", RightToLeft)
.AddClass(Class)
.Build();
[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get; set; }
[CascadingParameter] public TableContext Context { get; set; }
///
/// Set true to hide the part of the pager which allows to change the page size.
///
[Parameter] public bool HideRowsPerPage { get; set; }
///
/// Set true to hide the part of the pager which allows to change the page size.
///
[ExcludeFromCodeCoverage]
[Obsolete("Use HideRowsPerPage instead.", true)]
[Parameter] public bool DisableRowsPerPage { get => HideRowsPerPage; set => HideRowsPerPage = value; }
///
/// Set true to hide the number of pages.
///
[Parameter] public bool HidePageNumber { get; set; }
///
/// Set true to hide the pagination.
///
[Parameter] public bool HidePagination { get; set; }
///
/// Set the horizontal alignment position.
///
[Parameter] public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Right;
///
/// Define a list of available page size options for the user to choose from
///
[Parameter] public int[] PageSizeOptions { get; set; } = new int[] { 10, 25, 50, 100 };
///
/// Format string for the display of the current page, which you can localize to your language. Available variables are:
/// {first_item}, {last_item} and {all_items} which will replaced with the indices of the page's first and last item, as well as the total number of items.
/// Default: "{first_item}-{last_item} of {all_items}" which is transformed into "0-25 of 77".
///
[Parameter] public string InfoFormat { get; set; } = "{first_item}-{last_item} of {all_items}";
private string Info
{
get
{
// fetch number of filtered items (once only)
var filteredItemsCount = Table?.GetFilteredItemsCount() ?? 0;
return Table == null
? "Table==null"
: InfoFormat
.Replace("{first_item}", $"{(filteredItemsCount == 0 ? 0 : Table?.CurrentPage * Table.RowsPerPage + 1)}")
.Replace("{last_item}", $"{Math.Min((Table.CurrentPage + 1) * Table.RowsPerPage, filteredItemsCount)}")
.Replace("{all_items}", $"{filteredItemsCount}");
}
}
///
/// The localizable "Rows per page:" text.
///
[Parameter] public string RowsPerPageString { get; set; } = "Rows per page:";
///
/// Custom first icon.
///
[Parameter] public string FirstIcon { get; set; } = Icons.Material.Filled.FirstPage;
///
/// Custom before icon.
///
[Parameter] public string BeforeIcon { get; set; } = Icons.Material.Filled.NavigateBefore;
///
/// Custom next icon.
///
[Parameter] public string NextIcon { get; set; } = Icons.Material.Filled.NavigateNext;
///
/// Custom last icon.
///
[Parameter] public string LastIcon { get; set; } = Icons.Material.Filled.LastPage;
private void SetRowsPerPage(string size)
{
Table?.SetRowsPerPage(int.Parse(size));
}
private bool BackButtonsDisabled => Table == null ? false : Table.CurrentPage == 0;
private bool ForwardButtonsDisabled => Table == null ? false : (Table.CurrentPage + 1) * Table.RowsPerPage >= Table.GetFilteredItemsCount();
public TableBase Table => Context?.Table;
protected override void OnInitialized()
{
base.OnInitialized();
if (Context != null)
{
Context.HasPager = true;
Context.PagerStateHasChanged = StateHasChanged;
}
}
}