Skip to content

Commit 9d0635e

Browse files
github-actions[bot]KB Botxristianstefanov
authored
Merge new-kb-grid-kb-create-reusable-columns-565b883b49094b0d8ba5e32e057561f1-2587 into production (#2611)
* Added new kb article grid-kb-create-reusable-columns * kb(Grid): address comments --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Hristian Stefanov <Hristian.Stefanov@progress.com>
1 parent 961513a commit 9d0635e

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
<div class="skip-repl"></div>
35+
````Index.razor
36+
<TelerikGrid Data="@MyData" EditMode="@GridEditMode.Incell" Pageable="true" OnUpdate="@UpdateHandler">
37+
<GridColumns>
38+
<CustomColumn TItem="@Employee" Field="@(nameof(Employee.ID))"></CustomColumn>
39+
<CustomColumn TItem="@Employee" Field="@(nameof(Employee.Name))"></CustomColumn>
40+
<CustomColumn TItem="@Employee" EditedEmployee="@CurrentlyEditedEmployee" IsTemplate="@true" Field=@nameof(Employee.RoleId) Title="Position"></CustomColumn>
41+
</GridColumns>
42+
</TelerikGrid>
43+
44+
@code {
45+
private List<Employee> MyData { get; set; }
46+
private Employee CurrentlyEditedEmployee { get; set; }
47+
48+
private async Task UpdateHandler(GridCommandEventArgs args)
49+
{
50+
Employee item = (Employee)args.Item;
51+
52+
await MyService.Update(item);
53+
54+
await GetGridData();
55+
}
56+
57+
private async Task GetGridData()
58+
{
59+
MyData = await MyService.Read();
60+
}
61+
62+
protected override async Task OnInitializedAsync()
63+
{
64+
await GetGridData();
65+
}
66+
67+
public static class MyService
68+
{
69+
private static List<Employee> _data { get; set; } = new List<Employee>();
70+
71+
public static async Task<List<Employee>> Read()
72+
{
73+
if (_data.Count < 1)
74+
{
75+
for (int i = 0; i < 50; i++)
76+
{
77+
_data.Add(new Employee()
78+
{
79+
ID = i,
80+
Name = "name " + i,
81+
RoleId = i % 4
82+
});
83+
}
84+
}
85+
86+
return await Task.FromResult(_data);
87+
}
88+
89+
public static async Task Update(Employee itemToUpdate)
90+
{
91+
var index = _data.FindIndex(i => i.ID == itemToUpdate.ID);
92+
if (index != -1)
93+
{
94+
_data[index] = itemToUpdate;
95+
}
96+
}
97+
}
98+
}
99+
````
100+
````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+
````Employee.cs
157+
public class Employee
158+
{
159+
public int ID { get; set; }
160+
public string Name { get; set; }
161+
public int RoleId { get; set; }
162+
}
163+
````
164+
````Role.cs
165+
public class Role
166+
{
167+
public int RoleId { get; set; }
168+
public string RoleName { get; set; }
169+
}
170+
````
171+
172+
## See Also
173+
174+
- [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound)
175+
- [Grid Templates](https://docs.telerik.com/blazor-ui/components/grid/templates)

0 commit comments

Comments
 (0)