Skip to content

Instantly share code, notes, and snippets.

@TorreyGarland
Created March 18, 2022 23:09
Show Gist options
  • Save TorreyGarland/2d66f761431554e827f7213e0b3ffa9a to your computer and use it in GitHub Desktop.
Save TorreyGarland/2d66f761431554e827f7213e0b3ffa9a to your computer and use it in GitHub Desktop.
DataGrid Reads Data Twice Issue
@page "/fetchdata"
@using Highway1.Web.ViewModels
@using Highway1.Web.Data
/*@inherits ReactiveInjectableComponentBase<FetchDataViewModel>*/
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from a service.</p>
<DataGrid TItem="WeatherForecast"
Data="Data"
DetailRowStartsVisible="false"
Responsive
ReadData="ReadData"
Resizable
PageSize="10"
ShowPageSizes
ShowPager
UseInternalEditing="false"
Sortable="false"
HeaderThemeContrast="ThemeContrast.Dark"
TotalItems="50">
<DataGridColumns>
<DataGridColumn TItem="WeatherForecast"
Field="@nameof(WeatherForecast.Date)"
Caption="Date"
DisplayFormat="{0:d}" />
<DataGridColumn TItem="WeatherForecast"
Field="@nameof(WeatherForecast.TemperatureC)"
Caption="Temp. (C)" />
<DataGridColumn TItem="WeatherForecast"
Field="@nameof(WeatherForecast.TemperatureF)"
Caption="Temp. (F)" />
<DataGridColumn TItem="WeatherForecast"
Field="@nameof(WeatherForecast.Summary)"
Caption="Summary" />
</DataGridColumns>
<LoadingTemplate>
<Paragraph Italic>Loading...</Paragraph>
</LoadingTemplate>
</DataGrid>
namespace Highway1.Web.Pages;
public partial class FetchData
{
private Data.WeatherForecast[]? Data;
private readonly Data.WeatherForecastService _weatherForecastService = new();
private async Task ReadData(Blazorise.DataGrid.DataGridReadDataEventArgs<Data.WeatherForecast> e)
{
Data = await _weatherForecastService.GetForecastAsync(DateTime.Now, e.Page, e.PageSize, e.CancellationToken);
}
}
namespace Highway1.Web.Data;
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
namespace Highway1.Web.Data;
[ExportScoped<IWeatherForecastService>]
public class WeatherForecastService :
IWeatherForecastService
{
private static readonly ImmutableArray<string> Summaries
= ImmutableArray.Create(
"Freezing",
"Bracing",
"Chilly",
"Cool",
"Mild",
"Warm",
"Balmy",
"Hot",
"Sweltering",
"Scorching");
public Task<WeatherForecast[]> GetForecastAsync(
DateTime startDate,
int page,
int pageSize,
CancellationToken cancellationToken)
=> Task.FromResult(Enumerable.Range(1, 50)
.Select((index) => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.Skip(page)
.Take(pageSize)
.ToArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment