Files
egwcorelib/EgwCoreLib.BlazorTest/Pages/FetchData.razor.cs
T
2023-05-10 11:53:49 +02:00

122 lines
3.8 KiB
C#

using EgwCoreLib.BlazorTest.Data;
using EgwCoreLib.Razor;
namespace EgwCoreLib.BlazorTest.Pages
{
public partial class FetchData
{
#region Protected Properties
protected List<int> PageSizeEnab { get; set; } = new List<int>();
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
rawData = await ForecastService.GetForecastAsync(DateTime.Now);
PageSizeEnab = new List<int>()
{
4,8,12,16,20,24,28,32,36,40
};
await ReloadAllData();
}
protected async Task ReloadAllData()
{
await Task.Delay(1);
totalCount = rawData.Count();
List<WeatherForecast> currForecast = rawData.ToList();
// effetuo sort...
if (!string.IsNullOrEmpty(sortField))
{
switch (sortField)
{
case "TempC":
if (sortAsc)
{
currForecast = currForecast.OrderBy(x => x.TemperatureC).ToList();
}
else
{
currForecast = currForecast.OrderByDescending(x => x.TemperatureC).ToList();
}
break;
case "TempF":
if (sortAsc)
{
currForecast = currForecast.OrderBy(x => x.TemperatureF).ToList();
}
else
{
currForecast = currForecast.OrderByDescending(x => x.TemperatureF).ToList();
}
break;
case "Summary":
if (sortAsc)
{
currForecast = currForecast.OrderBy(x => x.Summary).ToList();
}
else
{
currForecast = currForecast.OrderByDescending(x => x.Summary).ToList();
}
break;
default:
case "Date":
if (sortAsc)
{
currForecast = currForecast.OrderBy(x => x.Date).ToList();
}
else
{
currForecast = currForecast.OrderByDescending(x => x.Date).ToList();
}
break;
}
}
forecasts = currForecast.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
}
protected async Task SetCurrPage(int newNum)
{
currPage = newNum;
await ReloadAllData();
}
protected async Task SetNumRec(int newNum)
{
numRecord = newNum;
await ReloadAllData();
}
protected async Task SortRequested(Sorter.SortCallBack e)
{
sortField = e.ParamName;
sortAsc = e.IsAscending;
await ReloadAllData();
}
#endregion Protected Methods
#region Private Fields
private List<WeatherForecast>? forecasts;
private WeatherForecast[] rawData { get; set; } = new WeatherForecast[1];
private bool sortAsc = true;
private string sortField = "";
#endregion Private Fields
#region Private Properties
private int currPage { get; set; } = 1;
private bool isLoading { get; set; } = false;
private int numRecord { get; set; } = 8;
private int totalCount { get; set; } = 0;
#endregion Private Properties
}
}