-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article grid-kb-create-reusable-columns #2587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xristianstefanov
merged 2 commits into
master
from
new-kb-grid-kb-create-reusable-columns-565b883b49094b0d8ba5e32e057561f1
Dec 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| --- | ||
| title: Reusable Grid Columns with Templates | ||
| description: Learn how to encapsulate GridColumn, including templates, into a reusable component for Telerik UI for Blazor Grid. | ||
| type: how-to | ||
| page_title: How to Make Reusable GridColumn Components with Templates in Blazor | ||
| slug: grid-kb-create-reusable-columns | ||
| tags: grid, blazor, gridcolumn, reusable component, templates, editor template | ||
| res_type: kb | ||
| ticketid: 1671641 | ||
| --- | ||
|
|
||
| ## Description | ||
| This knowledge base article answers the following questions: | ||
| - How can I create a reusable `GridColumn` component in Blazor? | ||
| - What is the best way to encapsulate `GridColumn` templates in a reusable component? | ||
|
|
||
| ## Environment | ||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Solution | ||
| To create a reusable `GridColumn` component with templates, follow these steps: | ||
|
|
||
| 1. Define a custom column component that accepts parameters for the field, title, whether it uses templates, and the currently edited item. | ||
|
|
||
| 2. Within the custom component, conditionally render a `GridColumn` with or without templates based on the provided parameters. | ||
|
|
||
| ````RAZOR Index.razor | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <TelerikGrid Data="@MyData" EditMode="@GridEditMode.Incell" Pageable="true" OnUpdate="@UpdateHandler"> | ||
| <GridColumns> | ||
| <CustomColumn TItem="@Employee" Field="@(nameof(Employee.ID))"></CustomColumn> | ||
| <CustomColumn TItem="@Employee" Field="@(nameof(Employee.Name))"></CustomColumn> | ||
| <CustomColumn TItem="@Employee" EditedEmployee="@CurrentlyEditedEmployee" IsTemplate="@true" Field=@nameof(Employee.RoleId) Title="Position"></CustomColumn> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private List<Employee> MyData { get; set; } | ||
| private Employee CurrentlyEditedEmployee { get; set; } | ||
|
|
||
| private async Task UpdateHandler(GridCommandEventArgs args) | ||
| { | ||
| Employee item = (Employee)args.Item; | ||
|
|
||
| await MyService.Update(item); | ||
|
|
||
| await GetGridData(); | ||
| } | ||
|
|
||
| private async Task GetGridData() | ||
| { | ||
| MyData = await MyService.Read(); | ||
| } | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| { | ||
| await GetGridData(); | ||
| } | ||
|
|
||
| public static class MyService | ||
| { | ||
| private static List<Employee> _data { get; set; } = new List<Employee>(); | ||
|
|
||
| public static async Task<List<Employee>> Read() | ||
| { | ||
| if (_data.Count < 1) | ||
| { | ||
| for (int i = 0; i < 50; i++) | ||
| { | ||
| _data.Add(new Employee() | ||
| { | ||
| ID = i, | ||
| Name = "name " + i, | ||
| RoleId = i % 4 | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return await Task.FromResult(_data); | ||
| } | ||
|
|
||
| public static async Task Update(Employee itemToUpdate) | ||
| { | ||
| var index = _data.FindIndex(i => i.ID == itemToUpdate.ID); | ||
| if (index != -1) | ||
| { | ||
| _data[index] = itemToUpdate; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ````RAZOR CustomColumn.razor | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @typeparam TItem | ||
|
|
||
| @if (IsTemplate) | ||
| { | ||
| <GridColumn Field="@Field" Title="@Title"> | ||
| <Template> | ||
| @{ | ||
| int roleId = (context as Employee).RoleId; | ||
| Role matchingPos = Roles.FirstOrDefault(r => r.RoleId == roleId); | ||
| string textToRender = matchingPos != null ? matchingPos.RoleName : "Unknown"; | ||
| <text>@textToRender</text> | ||
| } | ||
| </Template> | ||
| <EditorTemplate> | ||
| @{ | ||
| EditedEmployee = context as Employee; | ||
| <TelerikDropDownList Data="@Roles" | ||
| @bind-Value="@EditedEmployee.RoleId" | ||
| TextField="@nameof(Role.RoleName)" | ||
| ValueField="@nameof(Role.RoleId)" | ||
| DefaultText="Select Role"> | ||
| <DropDownListSettings> | ||
| <DropDownListPopupSettings Height="auto" /> | ||
| </DropDownListSettings> | ||
| </TelerikDropDownList> | ||
| } | ||
| </EditorTemplate> | ||
| </GridColumn> | ||
| } | ||
| else | ||
| { | ||
| <GridColumn Field="@Field"></GridColumn> | ||
| } | ||
|
|
||
| @code { | ||
| [Parameter] | ||
| public string Field { get; set; } | ||
|
|
||
| [Parameter] | ||
| public string Title { get; set; } | ||
|
|
||
| [Parameter] | ||
| public bool IsTemplate { get; set; } | ||
|
|
||
| [Parameter] | ||
| public Employee EditedEmployee { get; set; } | ||
|
|
||
| private List<Role> Roles { get; set; } = new List<Role> | ||
| { | ||
| new Role { RoleId = 1, RoleName = "Manager" }, | ||
| new Role { RoleId = 2, RoleName = "Employee" }, | ||
| new Role { RoleId = 3, RoleName = "Contractor" }, | ||
| }; | ||
| } | ||
| ```` | ||
|
|
||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ````C# Employee.cs | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public class Employee | ||
| { | ||
| public int ID { get; set; } | ||
| public string Name { get; set; } | ||
| public int RoleId { get; set; } | ||
| } | ||
| ```` | ||
| ````C# Role.cs | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public class Role | ||
| { | ||
| public int RoleId { get; set; } | ||
| public string RoleName { get; set; } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound) | ||
| - [Grid Templates](https://docs.telerik.com/blazor-ui/components/grid/templates) | ||
xristianstefanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.