Skip to content

Commit 4f7109c

Browse files
author
KB Bot
committed
Added new kb article grid-kb-create-reusable-columns
1 parent 3601745 commit 4f7109c

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
title: Reusable Grid Columns with Templates
3+
description: Learn how to encapsulate GridColumn, including templates, into a reusable component for Telerik UI for Blazor Grid.
4+
type: how-to
5+
page_title: How to Make Reusable GridColumn Components with Templates in Blazor
6+
slug: grid-kb-create-reusable-columns
7+
tags: grid, blazor, gridcolumn, reusable component, templates, editor template
8+
res_type: kb
9+
ticketid: 1671641
10+
---
11+
12+
## Description
13+
This knowledge base article answers the following questions:
14+
- How can I create a reusable `GridColumn` component in Blazor?
15+
- What is the best way to encapsulate `GridColumn` templates in a reusable component?
16+
17+
## Environment
18+
<table>
19+
<tbody>
20+
<tr>
21+
<td>Product</td>
22+
<td>Grid for Blazor</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Solution
28+
To create a reusable `GridColumn` component with templates, follow these steps:
29+
30+
1. Define a custom column component that accepts parameters for the field, title, whether it uses templates, and the currently edited item.
31+
32+
2. Within the custom component, conditionally render a `GridColumn` with or without templates based on the provided parameters.
33+
34+
````RAZOR Index.razor
35+
<TelerikGrid Data="@MyData" EditMode="@GridEditMode.Incell" Pageable="true" OnUpdate="@UpdateHandler">
36+
<GridColumns>
37+
<CustomColumn TItem="@Employee" Field="@(nameof(Employee.ID))"></CustomColumn>
38+
<CustomColumn TItem="@Employee" Field="@(nameof(Employee.Name))"></CustomColumn>
39+
<CustomColumn TItem="@Employee" EditedEmployee="@CurrentlyEditedEmployee" IsTemplate="@true" Field=@nameof(Employee.RoleId) Title="Position"></CustomColumn>
40+
</GridColumns>
41+
</TelerikGrid>
42+
43+
@code {
44+
private List<Employee> MyData { get; set; }
45+
private Employee CurrentlyEditedEmployee { get; set; }
46+
47+
private async Task UpdateHandler(GridCommandEventArgs args)
48+
{
49+
Employee item = (Employee)args.Item;
50+
51+
await MyService.Update(item);
52+
53+
await GetGridData();
54+
}
55+
56+
private async Task GetGridData()
57+
{
58+
MyData = await MyService.Read();
59+
}
60+
61+
protected override async Task OnInitializedAsync()
62+
{
63+
await GetGridData();
64+
}
65+
66+
public static class MyService
67+
{
68+
private static List<Employee> _data { get; set; } = new List<Employee>();
69+
70+
public static async Task<List<Employee>> Read()
71+
{
72+
if (_data.Count < 1)
73+
{
74+
for (int i = 0; i < 50; i++)
75+
{
76+
_data.Add(new Employee()
77+
{
78+
ID = i,
79+
Name = "name " + i,
80+
RoleId = i % 4
81+
});
82+
}
83+
}
84+
85+
return await Task.FromResult(_data);
86+
}
87+
88+
public static async Task Update(Employee itemToUpdate)
89+
{
90+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
91+
if (index != -1)
92+
{
93+
_data[index] = itemToUpdate;
94+
}
95+
}
96+
}
97+
}
98+
````
99+
100+
````RAZOR CustomColumn.razor
101+
@typeparam TItem
102+
103+
@if (IsTemplate)
104+
{
105+
<GridColumn Field="@Field" Title="@Title">
106+
<Template>
107+
@{
108+
int roleId = (context as Employee).RoleId;
109+
Role matchingPos = Roles.FirstOrDefault(r => r.RoleId == roleId);
110+
string textToRender = matchingPos != null ? matchingPos.RoleName : "Unknown";
111+
<text>@textToRender</text>
112+
}
113+
</Template>
114+
<EditorTemplate>
115+
@{
116+
EditedEmployee = context as Employee;
117+
<TelerikDropDownList Data="@Roles"
118+
@bind-Value="@EditedEmployee.RoleId"
119+
TextField="@nameof(Role.RoleName)"
120+
ValueField="@nameof(Role.RoleId)"
121+
DefaultText="Select Role">
122+
<DropDownListSettings>
123+
<DropDownListPopupSettings Height="auto" />
124+
</DropDownListSettings>
125+
</TelerikDropDownList>
126+
}
127+
</EditorTemplate>
128+
</GridColumn>
129+
}
130+
else
131+
{
132+
<GridColumn Field="@Field"></GridColumn>
133+
}
134+
135+
@code {
136+
[Parameter]
137+
public string Field { get; set; }
138+
139+
[Parameter]
140+
public string Title { get; set; }
141+
142+
[Parameter]
143+
public bool IsTemplate { get; set; }
144+
145+
[Parameter]
146+
public Employee EditedEmployee { get; set; }
147+
148+
private List<Role> Roles { get; set; } = new List<Role>
149+
{
150+
new Role { RoleId = 1, RoleName = "Manager" },
151+
new Role { RoleId = 2, RoleName = "Employee" },
152+
new Role { RoleId = 3, RoleName = "Contractor" },
153+
};
154+
}
155+
````
156+
157+
````C# Employee.cs
158+
public class Employee
159+
{
160+
public int ID { get; set; }
161+
public string Name { get; set; }
162+
public int RoleId { get; set; }
163+
}
164+
````
165+
````C# Role.cs
166+
public class Role
167+
{
168+
public int RoleId { get; set; }
169+
public string RoleName { get; set; }
170+
}
171+
````
172+
173+
## See Also
174+
175+
- [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound)
176+
- [Grid Templates](https://docs.telerik.com/blazor-ui/components/grid/templates)

0 commit comments

Comments
 (0)