|
| 1 | +--- |
| 2 | +title: Using Components in Grid Templates |
| 3 | +description: How to use my custom component in the various UI for Blazor Grid templates |
| 4 | +type: how-to |
| 5 | +page_title: Using Components in Grid Templates |
| 6 | +slug: grid-kb-using-components-in-templates |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Grid for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I'm trying to render my own component in the Column Template of the Grid. However, the component does not show correct values after filtering, paging or sorting. |
| 27 | + |
| 28 | +How to properly use components in Grid templates? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +The Grid optimizes the UI renders after data operations. If you are using child components inside Grid column templates, [set the `@key` attribute to these components to ensure that they always show the correct values and content after filtering, paging, and sorting](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/element-component-model-relationships?view=aspnetcore-9.0). |
| 33 | + |
| 34 | +This applies to the following Grid elements: |
| 35 | + |
| 36 | +* [CommandColumn]({%slug components/grid/columns/command%}) |
| 37 | +* [DetailTemplate]({%slug components/grid/features/hierarchy%}) |
| 38 | +* [FooterTemplate]({%slug grid-templates-column-footer%}) |
| 39 | +* [GroupFooterTemplate]({%slug grid-templates-column-group-footer%}) |
| 40 | +* [RowTemplate]({%slug grid-templates-row%}) |
| 41 | +* [Template]({%slug grid-templates-column%}) |
| 42 | + |
| 43 | +>caption Setting @key to child components inside Grid templates |
| 44 | +
|
| 45 | +<div class="skip-repl"></div> |
| 46 | +````RAZOR Home.razor |
| 47 | +@using YourAppName.Data |
| 48 | +
|
| 49 | +<ul> |
| 50 | + <li>Filter and sort the Grid to see the difference between the two columns.</li> |
| 51 | + <li>Group the Grid by the first column to test the GroupFooterTemplate.</li> |
| 52 | + <li>Uncomment the RowTemplate to see it in action. </li> |
| 53 | +</ul> |
| 54 | +
|
| 55 | +<TelerikGrid Data="@GridData" |
| 56 | + EditMode="@GridEditMode.Inline" |
| 57 | + FilterMode="GridFilterMode.FilterRow" |
| 58 | + Groupable="true" |
| 59 | + Pageable="true" |
| 60 | + PageSize="5" |
| 61 | + Sortable="true"> |
| 62 | + <GridAggregates> |
| 63 | + <GridAggregate Field="@nameof(SampleModel.Id)" FieldType="@typeof(int)" Aggregate="@GridAggregateType.Max" /> |
| 64 | + </GridAggregates> |
| 65 | + <GridColumns> |
| 66 | + <GridColumn Field="@nameof(SampleModel.Id)" Title="Template with Key"> |
| 67 | + <Template> |
| 68 | + @{ |
| 69 | + var dataItem = (SampleModel)context; |
| 70 | + } |
| 71 | + Direct: @dataItem.Id |
| 72 | + <br /> |
| 73 | + In child: |
| 74 | + <Child @key="@dataItem" Model="@dataItem" Color="green" /> |
| 75 | + </Template> |
| 76 | + <FooterTemplate> |
| 77 | + Direct: @context.Max |
| 78 | + <br /> |
| 79 | + In child: |
| 80 | + <Child Model="@( new SampleModel() { Id = Convert.ToInt32(context.Max) })" Color="green" /> |
| 81 | + </FooterTemplate> |
| 82 | + <GroupFooterTemplate> |
| 83 | + Direct: @context.Max |
| 84 | + <br /> |
| 85 | + In child: |
| 86 | + <Child Model="@( new SampleModel() { Id = Convert.ToInt32(context.Max) })" Color="green" /> |
| 87 | + </GroupFooterTemplate> |
| 88 | + </GridColumn> |
| 89 | + <GridColumn Field="@nameof(SampleModel.Name)" Title="Template without Key"> |
| 90 | + <Template> |
| 91 | + @{ |
| 92 | + var dataItem = (SampleModel)context; |
| 93 | + } |
| 94 | + Direct: @dataItem.Id |
| 95 | + <br /> |
| 96 | + In child: |
| 97 | + <Child Model="@dataItem" Color="red" /> |
| 98 | + </Template> |
| 99 | + </GridColumn> |
| 100 | + <GridCommandColumn> |
| 101 | + <GridCommandButton Command="Edit" Icon="@SvgIcon.Pencil" /> |
| 102 | + <GridCommandButton Command="Cancel" Icon="@SvgIcon.Cancel" ShowInEdit="true" /> |
| 103 | + @{ |
| 104 | + var dataItem = (SampleModel)context; |
| 105 | + } |
| 106 | + Direct: @dataItem.Id |
| 107 | + <br /> |
| 108 | + In child: |
| 109 | + <Child @key="@dataItem" Model="@dataItem" /> |
| 110 | + </GridCommandColumn> |
| 111 | + </GridColumns> |
| 112 | + @* <RowTemplate> |
| 113 | + @{ |
| 114 | + var dataItem = (SampleModel)context; |
| 115 | + } |
| 116 | + <td> |
| 117 | + Direct: @dataItem.Id |
| 118 | + <br /> |
| 119 | + In child: |
| 120 | + <Child @key="@dataItem" Model="@dataItem" /> |
| 121 | + </td> |
| 122 | + <td>@dataItem.Name</td> |
| 123 | + </RowTemplate> *@ |
| 124 | + <DetailTemplate> |
| 125 | + @{ |
| 126 | + var masterItem = (SampleModel)context; |
| 127 | + } |
| 128 | + <p><strong>DetailTemplate</strong></p> |
| 129 | + Direct: @masterItem.Id |
| 130 | + <br /> |
| 131 | + In child: <Child @key="@masterItem" Model="@masterItem" /> |
| 132 | + </DetailTemplate> |
| 133 | +</TelerikGrid> |
| 134 | +
|
| 135 | +@code { |
| 136 | + private List<SampleModel> GridData { get; set; } = new(); |
| 137 | +
|
| 138 | + protected override void OnInitialized() |
| 139 | + { |
| 140 | + for (int i = 1; i <= 77; i++) |
| 141 | + { |
| 142 | + GridData.Add(new SampleModel() |
| 143 | + { |
| 144 | + Id = i, |
| 145 | + Name = $"Name {i}" |
| 146 | + }); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +```` |
| 151 | +````RAZOR Child.razor |
| 152 | +@using YourAppName.Data |
| 153 | +
|
| 154 | +Properties require @@key: |
| 155 | +<strong style="color: @Color; background: yellow; padding: 0 .2em;">@Foo</strong> |
| 156 | +
|
| 157 | +<br /> |
| 158 | +
|
| 159 | +Parameters refresh: |
| 160 | +<strong>@Model.Id</strong> |
| 161 | +
|
| 162 | +@code { |
| 163 | + [Parameter] |
| 164 | + public SampleModel Model { get; set; } = new(); |
| 165 | +
|
| 166 | + [Parameter] |
| 167 | + public string Color { get; set; } = "inherit"; |
| 168 | +
|
| 169 | + private string Foo { get; set; } = string.Empty; |
| 170 | +
|
| 171 | + protected override void OnInitialized() |
| 172 | + { |
| 173 | + Foo = Model.Id.ToString(); |
| 174 | + } |
| 175 | +} |
| 176 | +```` |
| 177 | +````C# SampleModel.cs |
| 178 | +namespace YourAppName.Data |
| 179 | +{ |
| 180 | + public class SampleModel |
| 181 | + { |
| 182 | + public int Id { get; set; } |
| 183 | + public string Name { get; set; } = string.Empty; |
| 184 | + } |
| 185 | +} |
| 186 | +```` |
0 commit comments