/
Index.razor
73 lines (62 loc) · 2.91 KB
/
Index.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@page "/"
@using ServerLocalizationResx.Data
@using Telerik.Blazor.Services
@inject WeatherForecastServiceMod ForecastService
@* You can use the service for Telerik localization for your own code as well - for example the grid command buttons *@
@inject ITelerikStringLocalizer Localizer
@* This is the namespace that is generated from the resx files localizer *@
@using ServerLocalizationResx.Resources
<CultureChooser />
<TelerikGrid Data="@forecasts"
Pageable="true" Groupable="true" Sortable="true"
OnUpdate="@UpdateHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler">
<GridToolBarTemplate>
<GridCommandButton Command="Add" Icon="@SvgIcon.Plus">@Localizer[nameof(TelerikMessages.Grid_Create)]</GridCommandButton>
</GridToolBarTemplate>
<GridColumns>
<GridColumn Field="@(nameof(WeatherForecast.Date))">
<Template>
@((context as WeatherForecast).Date.ToShortDateString())
</Template>
</GridColumn>
<GridColumn Field="@(nameof(WeatherForecast.TemperatureC))" />
<GridColumn Field="@(nameof(WeatherForecast.TemperatureF))" />
<GridColumn Field="@(nameof(WeatherForecast.Summary))" />
<GridCommandColumn>
<GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil">@Localizer[nameof(TelerikMessages.Grid_Edit)]</GridCommandButton>
<GridCommandButton Command="Delete" Icon="@SvgIcon.Trash">@Localizer[nameof(TelerikMessages.Grid_Delete)]</GridCommandButton>
<GridCommandButton Command="Save" Icon="@SvgIcon.Save" ShowInEdit="true">@Localizer[nameof(TelerikMessages.Grid_Update)]</GridCommandButton>
<GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true">@Localizer[nameof(TelerikMessages.Grid_Cancel)]</GridCommandButton>
</GridCommandColumn>
</GridColumns>
</TelerikGrid>
@code {
List<WeatherForecast> forecasts { get; set; } = new List<WeatherForecast>();
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(150);
}
// sample CUD operations to showcase how to localize command buttons as well
public void UpdateHandler(GridCommandEventArgs args)
{
WeatherForecast item = (WeatherForecast)args.Item;
var matchingItem = forecasts.FirstOrDefault(c => c.Id == item.Id);
if (matchingItem != null)
{
matchingItem.Summary = item.Summary;
matchingItem.TemperatureC = item.TemperatureC;
matchingItem.Date = item.Date;
}
}
public void DeleteHandler(GridCommandEventArgs args)
{
WeatherForecast item = (WeatherForecast)args.Item;
forecasts.Remove(item);
}
public void CreateHandler(GridCommandEventArgs args)
{
WeatherForecast item = (WeatherForecast)args.Item;
item.Id = forecasts.Count;
forecasts.Insert(0, item);
}
}