Repository navigation
Expand file tree
/
Copy pathIndex.razor
More file actions
127 lines (114 loc) · 4.41 KB
/
Copy pathIndex.razor
File metadata and controls
127 lines (114 loc) · 4.41 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
@page "/"
@using Grid.Northwind
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable
<DxGrid Data="Data"
CustomizeEditModel="OnCustomizeEditModel"
EditModelSaving="OnEditModelSaving"
DataItemDeleting="OnDataItemDeleting"
KeyFieldName="EmployeeId"
EditMode="GridEditMode.PopupEditForm"
FocusedRowEnabled=true
@ref=MyGrid>
<Columns>
<DxGridDataColumn FieldName="FirstName" />
<DxGridDataColumn FieldName="LastName" />
<DxGridDataColumn FieldName="Title" />
<DxGridDataColumn FieldName="HireDate" />
</Columns>
<ToolbarTemplate>
<DxToolbar>
<DxToolbarItem Text="New"
IconCssClass="grid-new-row"
Click="OnCreateNew" />
<DxToolbarItem Text="Edit"
IconCssClass="grid-edit-row"
Click="OnEdit" />
<DxToolbarItem Text="Delete"
IconCssClass="grid-delete-row"
Click="OnDelete" />
<DxToolbarItem Text="Column Chooser"
RenderStyle="ButtonRenderStyle.Secondary"
IconCssClass="grid-column-chooser"
CssClass="column-chooser-button"
BeginGroup="true"
Click="OnShowColumnChooser" />
</DxToolbar>
</ToolbarTemplate>
<EditFormTemplate Context="editFormContext">
<DxFormLayout>
<DxFormLayoutItem Caption="First Name:">
@editFormContext.GetEditor("FirstName")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Last Name:">
@editFormContext.GetEditor("LastName")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Title:">
@editFormContext.GetEditor("Title")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Hire Date:">
@editFormContext.GetEditor("HireDate")
</DxFormLayoutItem>
<DxFormLayoutItem ColSpanMd="12">
<ValidationSummary />
</DxFormLayoutItem>
</DxFormLayout>
</EditFormTemplate>
</DxGrid>
@code {
IGrid MyGrid { get; set; }
IEnumerable<object> Data { get; set; }
NorthwindContext Northwind { get; set; }
protected override void OnInitialized() {
Northwind = NorthwindContextFactory.CreateDbContext();
Data = Northwind.Employees
.ToList();
}
public void Dispose() {
Northwind?.Dispose();
}
void OnCustomizeEditModel(GridCustomizeEditModelEventArgs e) {
if (e.IsNew) {
var editModel = (Employee)e.EditModel;
editModel.EmployeeId = Northwind.Employees.Max(x => x.EmployeeId) + 1;
}
}
async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
var editModel = (Employee)e.EditModel;
// Assign changes from the edit model to the data item.
if (e.IsNew)
await Northwind.AddAsync(editModel);
else
e.CopyChangesToDataItem();
// Post changes to the database.
await Northwind.SaveChangesAsync();
// Reload the entire Grid.
Data = await Northwind.Employees.ToListAsync();
}
async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
// Re-query a data item from the database.
var dataItem = Northwind.Employees.Find((e.DataItem as Employee).EmployeeId);
if (dataItem != null) {
// Remove the data item from the database.
Northwind.Remove(dataItem);
await Northwind.SaveChangesAsync();
// Reload the entire Grid.
Data = await Northwind.Employees.ToListAsync();
}
}
async Task OnCreateNew(ToolbarItemClickEventArgs e) {
await MyGrid.StartEditNewRowAsync();
}
async Task OnEdit(ToolbarItemClickEventArgs e) {
if (MyGrid.GetFocusedDataItem() != null)
await MyGrid.StartEditDataItemAsync(MyGrid.GetFocusedDataItem());
}
void OnDelete(ToolbarItemClickEventArgs e) {
if (MyGrid.GetFocusedDataItem() != null)
MyGrid.ShowDataItemDeleteConfirmation(MyGrid.GetFocusedDataItem());
}
void OnShowColumnChooser() {
MyGrid.ShowColumnChooser(".column-chooser-button");
}
}