要在C# Blazor中實現分頁功能,您可以使用Blazor組件和C#代碼來實現。以下是一種簡單的方法:
@page "/pagination"
<div>
<button disabled="@IsFirstPage" @onclick="GoToFirstPage">First</button>
<button disabled="@IsFirstPage" @onclick="GoToPreviousPage">Previous</button>
<span>Page @CurrentPage of @TotalPages</span>
<button disabled="@IsLastPage" @onclick="GoToNextPage">Next</button>
<button disabled="@IsLastPage" @onclick="GoToLastPage">Last</button>
</div>
@code {
// Define properties
private int CurrentPage { get; set; } = 1;
private int PageSize { get; set; } = 10;
private int TotalPages { get; set; } = 0;
private bool IsFirstPage => CurrentPage == 1;
private bool IsLastPage => CurrentPage == TotalPages;
// Define methods
private void GoToFirstPage()
{
CurrentPage = 1;
UpdatePageData();
}
private void GoToPreviousPage()
{
CurrentPage--;
UpdatePageData();
}
private void GoToNextPage()
{
CurrentPage++;
UpdatePageData();
}
private void GoToLastPage()
{
CurrentPage = TotalPages;
UpdatePageData();
}
private void UpdatePageData()
{
// Update page data, e.g. fetch data from backend API
}
}
@page "/pagedata"
<Pagination />
@code {
}
以上示例展示了一個簡單的分頁組件和如何在Blazor頁面中使用它。您可以根據您的需求和項目要求來擴展和定制這個分頁組件。