Skip to content

Commit 2f90412

Browse files
chore(grid): foreign key example in editor template
1 parent 110ca4d commit 2f90412

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

components/grid/templates/editor.md

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@ You can data bind components in it to the current context, which is an instance
1616

1717
If you need to perform logic more complex than simple data binding, use the change event of the custom editor component to perform it. You can also consider using a [custom edit form](https://demos.telerik.com/blazor-ui/grid/editing-custom-form).
1818

19-
>caption Sample editor template for a field
19+
>caption Sample editor template for a field - limit the string input options through a select element
2020
2121
````CSHTML
22-
@* This example shows how to use a dropdownlist to edit strings. You can implement any desired logic instead.
23-
If you have an enum, the grid can edit and filter it out-of-the-box without the need for an edit template *@
22+
@* This example shows how to use a simple <select> to edit strings. You can implement any desired logic instead.
23+
If you have an enum, the grid can edit and filter it out-of-the-box without the need for an edit template *@
2424
25-
<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="500px" OnUpdate="@UpdateHandler">
25+
<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="300px" OnUpdate="@UpdateHandler">
2626
<GridColumns>
2727
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="ID" />
2828
<GridColumn Field=@nameof(SampleData.Name) Title="Name" />
2929
<GridColumn Field=@nameof(SampleData.Role) Title="Position">
3030
<EditorTemplate>
3131
@{
3232
CurrentlyEditedEmployee = context as SampleData;
33-
<TelerikDropDownList Data="@Roles" @bind-Value="CurrentlyEditedEmployee.Role" Width="120px" PopupHeight="auto"></TelerikDropDownList>
33+
<select @bind="@CurrentlyEditedEmployee.Role">
34+
@foreach (var item in Roles)
35+
{
36+
<option value="@item">@item</option>
37+
}
38+
</select>
3439
}
3540
</EditorTemplate>
3641
</GridColumn>
@@ -88,9 +93,120 @@ If you have an enum, the grid can edit and filter it out-of-the-box without the
8893
}
8994
````
9095

91-
>caption The result from the code snippet above, after Edit was clicked on the first row and the user expanded the dropdown from the template
96+
>caption The result from the snippet above after Edit was clicked for the first row and the select was expanded
97+
98+
![editor template for simple strings with a select element](images/edit-template-simple-string-select.png)
99+
100+
>caption Sample editor template that uses a foreign key
101+
102+
````CSHTML
103+
@* This example shows one way to use a dropdownlist to edit values with a foreign key. *@
104+
105+
<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="500px" OnUpdate="@UpdateHandler">
106+
<GridColumns>
107+
<GridColumn Field=@nameof(Employee.ID) Editable="false" Title="ID" />
108+
<GridColumn Field=@nameof(Employee.Name) Title="Name" />
109+
<GridColumn Field=@nameof(Employee.RoleId) Title="Position">
110+
<EditorTemplate>
111+
@{
112+
CurrentlyEditedEmployee = context as Employee;
113+
<TelerikDropDownList Data="@Roles" DefaultText="Select Role"
114+
@bind-Value="@CurrentlyEditedEmployee.RoleId"
115+
TextField="@nameof(Role.RoleName)" ValueField="@nameof(Role.RoleId)"
116+
Width="100%" PopupHeight="auto">
117+
</TelerikDropDownList>
118+
}
119+
</EditorTemplate>
120+
<Template>
121+
@{
122+
// a Template is used to show the foreign key data that is user-friendly
123+
int roleId = (context as Employee).RoleId;
124+
Role matchingPos = Roles.FirstOrDefault(r => r.RoleId == roleId);
125+
string textToRender = matchingPos != null ? matchingPos.RoleName : "Unknown";
126+
<text>@textToRender</text>
127+
}
128+
</Template>
129+
</GridColumn>
130+
<GridCommandColumn>
131+
<GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
132+
<GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
133+
</GridCommandColumn>
134+
</GridColumns>
135+
</TelerikGrid>
136+
137+
@code {
138+
List<Employee> MyData { get; set; }
139+
List<Role> Roles { get; set; }
140+
public Employee CurrentlyEditedEmployee { get; set; }
141+
142+
public void UpdateHandler(GridCommandEventArgs args)
143+
{
144+
Employee item = (Employee)args.Item;
145+
146+
//perform actual data source operations here
147+
148+
var index = MyData.FindIndex(i => i.ID == item.ID);
149+
if (index != -1)
150+
{
151+
MyData[index] = item;
152+
}
153+
}
154+
155+
protected override async Task OnInitializedAsync()
156+
{
157+
Roles = await GetRoles();
158+
MyData = await GetGridData();
159+
}
160+
161+
async Task<List<Role>> GetRoles()
162+
{
163+
var data = new List<Role>
164+
{
165+
new Role { RoleId = 1, RoleName = "Manager" },
166+
new Role { RoleId = 2, RoleName = "Employee" },
167+
new Role { RoleId = 3, RoleName = "Contractor" },
168+
};
169+
170+
return await Task.FromResult(data);
171+
}
172+
173+
async Task<List<Employee>> GetGridData()
174+
{
175+
var data = new List<Employee>();
176+
for (int i = 0; i < 50; i++)
177+
{
178+
data.Add(new Employee()
179+
{
180+
ID = i,
181+
Name = "name " + i,
182+
RoleId = i % 5 // every one in four is an unknown one that will not be present in the roles list
183+
// and will have an ID of 0 to match the DefaultText of the dropdownlist
184+
// you can perform more complicated checks as necessary in your app and/or in the templates
185+
// and/or in the view-model data to present it with suitable values and avoid exceptions
186+
});
187+
}
188+
189+
return await Task.FromResult(data);
190+
}
191+
192+
public class Employee
193+
{
194+
public int ID { get; set; }
195+
public string Name { get; set; }
196+
public int RoleId { get; set; }
197+
}
198+
199+
public class Role
200+
{
201+
public int RoleId { get; set; }
202+
public string RoleName { get; set; }
203+
}
204+
}
205+
````
206+
207+
>caption The result from the code snippet above, after Edit was clicked on the second row and the user expanded the dropdown from the editor template
92208
93-
![](images/edit-template.png)
209+
![Editor Template for a foreign key](images/edit-template-foreign-key.png)
94210

95211
## See Also
96212

17.7 KB
Loading
17.9 KB
Loading
-26.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)