-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.razor
More file actions
129 lines (117 loc) · 4.8 KB
/
Index.razor
File metadata and controls
129 lines (117 loc) · 4.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
@page "/"
@using BlazorGridPopupEditFormCustomization.Services
@rendermode InteractiveServer
@inject WeatherForecastService ForecastService
<DxGrid @ref=grid Data="@forecasts">
<Columns>
<DxGridCommandColumn>
<HeaderTemplate>
<DxButton CssClass="w-100" RenderStyle="ButtonRenderStyle.Link"
Click="() => ShowPopup(new WeatherForecast())"
Text="New"></DxButton>
</HeaderTemplate>
<CellDisplayTemplate>
<DxButton RenderStyle="ButtonRenderStyle.Link"
Click="() => ShowPopup(context.DataItem)"
Text="Edit"></DxButton>
</CellDisplayTemplate>
</DxGridCommandColumn>
<DxGridDataColumn Caption="Date" FieldName="Date" />
<DxGridDataColumn Caption="Temperature (C)" FieldName="TemperatureC" />
<DxGridDataColumn Caption="Temperature (F)" FieldName="TemperatureF" />
<DxGridDataColumn Caption="Summary" FieldName="Summary" />
</Columns>
</DxGrid>
@if(PopupVisible) {
<DxPopup Visible="PopupVisible"
VisibleChanged="OnVisibleChanged"
AllowDrag="true"
AllowResize="true"
CloseOnEscape="false"
HeaderText="Custom Edit Form"
Width="600px">
<BodyContentTemplate Context="popupContext">
<EditForm Model="@editModel" Context="editFormContext" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator></DataAnnotationsValidator>
<DxFormLayout Data="@editModel" ItemUpdating="@((pair) => OnItemUpdating(pair.Key, pair.Value))">
<DxFormLayoutItem Caption="Date" Field="Date" />
<DxFormLayoutItem Caption="Temperature C" Field="TemperatureC" />
<DxFormLayoutItem ReadOnly=true Caption="Temperature F" Field="TemperatureF" />
<DxFormLayoutItem Caption="Summary" Field="Summary" />
<DxFormLayoutItem ColSpanLg="12">
<div class="w-100" style="display: flex; justify-content: end; gap: 1rem;">
<DxButton RenderStyle=ButtonRenderStyle.Primary Text="Save" SubmitFormOnClick=true></DxButton>
<DxButton RenderStyle=ButtonRenderStyle.Secondary Text="Cancel" Click="ClosePopup"></DxButton>
</div>
</DxFormLayoutItem>
</DxFormLayout>
</EditForm>
</BodyContentTemplate>
</DxPopup>
}
@code {
private DxGrid? grid;
private WeatherForecast? editModel;
private List<WeatherForecast> forecasts = [];
private bool PopupVisible => editModel != null;
private bool IsNew => !forecasts.Any(f => f.ID == editModel!.ID);
private void OnItemUpdating(string key, object value) {
switch(key) {
case nameof(WeatherForecast.TemperatureC):
if(value is int temperatureC)
editModel!.TemperatureC = temperatureC;
break;
case nameof(WeatherForecast.Date):
if(value is DateTime date)
editModel!.Date = date;
break;
case nameof(WeatherForecast.Summary):
if(value is string summary)
editModel!.Summary = summary;
break;
default:
throw new InvalidOperationException($"Unknown field: {key}");
}
}
private void OnValidSubmit(EditContext ctx) {
if(ctx.Model is not WeatherForecast wf) return;
if(IsNew)
InsertRecord(wf);
else
UpdateRecord(wf);
grid?.Reload();
ClosePopup();
}
private void InsertRecord(WeatherForecast wf) {
wf.ID = forecasts.Max(f => f.ID) + 1;
forecasts.Add(wf);
}
private void UpdateRecord(WeatherForecast wf) {
var itemToUpdate = forecasts!.FirstOrDefault(f => f.ID == wf.ID);
if(itemToUpdate != null) {
itemToUpdate.TemperatureC = wf.TemperatureC;
itemToUpdate.Date = wf.Date;
itemToUpdate.Summary = wf.Summary;
}
}
private void ShowPopup(object dataItem) {
if(dataItem is not WeatherForecast wf)
throw new InvalidOperationException("Invalid data item type.");
editModel = new WeatherForecast() {
ID = wf.ID,
Date = wf.Date,
TemperatureC = wf.TemperatureC,
Summary = wf.Summary
};
}
private void ClosePopup() {
editModel = null;
}
private void OnVisibleChanged(bool visible) {
if(!visible)
ClosePopup();
}
protected override async Task OnInitializedAsync() {
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}