diff --git a/_contentTemplates/grid/common-link.md b/_contentTemplates/grid/common-link.md index 4b4b40dd26..b762efad1d 100644 --- a/_contentTemplates/grid/common-link.md +++ b/_contentTemplates/grid/common-link.md @@ -211,3 +211,7 @@ Therefore, we advise that you do not set `Locked=false` for child columns of loc The keyboard navigation in the multi-column headers follows the functionality of Excel. #end + +#using-components-in-templates +See specifics and example in this knowledge base article: [Using Components in Grid Templates]({%slug grid-kb-using-components-in-templates%}). +#end \ No newline at end of file diff --git a/components/grid/columns/command.md b/components/grid/columns/command.md index b58e21fd95..081cb8d418 100644 --- a/components/grid/columns/command.md +++ b/components/grid/columns/command.md @@ -219,6 +219,10 @@ The following code example demonstrates declarations and handling. ![Blazor Grid Command Column Result](images/command-column-result.png) +## Using Components in Grid Command Column + +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) + ## See Also * [Live Demo: Grid Command Column](https://demos.telerik.com/blazor-ui/grid/editing-inline) diff --git a/components/grid/hierarchy.md b/components/grid/hierarchy.md index f0ae8f8ec3..94f404c460 100644 --- a/components/grid/hierarchy.md +++ b/components/grid/hierarchy.md @@ -119,6 +119,9 @@ The following articles and sample projects can be helpful when implementing hier * [Custom Excel Export that Includes Hierarchy](https://github.com/telerik/blazor-ui/tree/master/grid/export-to-xlsx-hierarchy) +## Using Components in Grid Detail Templates + +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) ## See Also diff --git a/components/grid/templates/column-footer.md b/components/grid/templates/column-footer.md index f81db66660..2f23855e89 100644 --- a/components/grid/templates/column-footer.md +++ b/components/grid/templates/column-footer.md @@ -82,6 +82,9 @@ You can use [aggregates]({%slug grid-aggregates%}) for the current field directl ![Blazor Grid Footer Template](images/footer-template.png) +## Using Components in Grid Column Footer Templates + +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) ## Notes diff --git a/components/grid/templates/column-group-footer.md b/components/grid/templates/column-group-footer.md index 54bd48a5ca..71b524d327 100644 --- a/components/grid/templates/column-group-footer.md +++ b/components/grid/templates/column-group-footer.md @@ -79,6 +79,9 @@ When the grid is grouped, the columns can display a footer with information abou ![Blazor Grid Column Group Footer Template](images/column-group-footer-template.png) +## Using Components in Grid Group Footer Templates + +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) ## See Also diff --git a/components/grid/templates/column.md b/components/grid/templates/column.md index d7457b70ef..b98626292e 100644 --- a/components/grid/templates/column.md +++ b/components/grid/templates/column.md @@ -100,87 +100,7 @@ The example below shows how to: ## Using Components in Grid Column Templates -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. - ->caption Seting @key to child components inside a Grid column template -
-````RAZOR Home.razor -@using YourAppName.Data - - - - - - - - - - - - -@code { - private List GridData { get; set; } = new(); - - protected override void OnInitialized() - { - for (int i = 1; i <= 77; i++) - { - GridData.Add(new SampleModel() - { - Id = i, - Name = $"Name {i}" - }); - } - } -} -```` -````RAZOR Child.razor -@using YourAppName.Data - -Properties require @@key: -@Foo - -
- -Parameters refresh: -@Model.Id - -@code { - [Parameter] - public SampleModel Model { get; set; } = new(); - - [Parameter] - public string Color { get; set; } = "inherit"; - - private string Foo { get; set; } = string.Empty; - - protected override void OnInitialized() - { - Foo = Model.Id.ToString(); - } -} -```` -````C# SampleModel.cs -namespace YourAppName.Data -{ - public class SampleModel - { - public int Id { get; set; } - public string Name { get; set; } = string.Empty; - } -} -```` +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) ## See Also diff --git a/components/grid/templates/row.md b/components/grid/templates/row.md index 3bbccb7912..a5ae128b90 100644 --- a/components/grid/templates/row.md +++ b/components/grid/templates/row.md @@ -62,6 +62,10 @@ Render the entire row with your own code and logic ![Blazor Grid Row Template](images/row-template.png) +## Using Components in Grid Row Templates + +@[template](/_contentTemplates/grid/common-link.md#using-components-in-templates) + ## See Also diff --git a/knowledge-base/grid-using-components-in-templates.md b/knowledge-base/grid-using-components-in-templates.md new file mode 100644 index 0000000000..1e0d491257 --- /dev/null +++ b/knowledge-base/grid-using-components-in-templates.md @@ -0,0 +1,186 @@ +--- +title: Using Components in Grid Templates +description: How to use my custom component in the various UI for Blazor Grid templates +type: how-to +page_title: Using Components in Grid Templates +slug: grid-kb-using-components-in-templates +position: +tags: +ticketid: +res_type: kb +--- + +## Environment + + + + + + + + +
ProductGrid for Blazor
+ +## Description + +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. + +How to properly use components in Grid templates? + +## Solution + +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). + +This applies to the following Grid elements: + +* [CommandColumn]({%slug components/grid/columns/command%}) +* [DetailTemplate]({%slug components/grid/features/hierarchy%}) +* [FooterTemplate]({%slug grid-templates-column-footer%}) +* [GroupFooterTemplate]({%slug grid-templates-column-group-footer%}) +* [RowTemplate]({%slug grid-templates-row%}) +* [Template]({%slug grid-templates-column%}) + +>caption Setting @key to child components inside Grid templates + +
+````RAZOR Home.razor +@using YourAppName.Data + + + + + + + + + + + + Direct: @context.Max +
+ In child: + +
+ + Direct: @context.Max +
+ In child: + +
+
+ + + + + + + @{ + var dataItem = (SampleModel)context; + } + Direct: @dataItem.Id +
+ In child: + +
+
+ @* + @{ + var dataItem = (SampleModel)context; + } + + Direct: @dataItem.Id +
+ In child: + + + @dataItem.Name +
*@ + + @{ + var masterItem = (SampleModel)context; + } +

DetailTemplate

+ Direct: @masterItem.Id +
+ In child: +
+
+ +@code { + private List GridData { get; set; } = new(); + + protected override void OnInitialized() + { + for (int i = 1; i <= 77; i++) + { + GridData.Add(new SampleModel() + { + Id = i, + Name = $"Name {i}" + }); + } + } +} +```` +````RAZOR Child.razor +@using YourAppName.Data + +Properties require @@key: +@Foo + +
+ +Parameters refresh: +@Model.Id + +@code { + [Parameter] + public SampleModel Model { get; set; } = new(); + + [Parameter] + public string Color { get; set; } = "inherit"; + + private string Foo { get; set; } = string.Empty; + + protected override void OnInitialized() + { + Foo = Model.Id.ToString(); + } +} +```` +````C# SampleModel.cs +namespace YourAppName.Data +{ + public class SampleModel + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + } +} +```` \ No newline at end of file diff --git a/upgrade/breaking-changes/7-0-0.md b/upgrade/breaking-changes/7-0-0.md index 1df4f2a1f2..f02126ab6b 100644 --- a/upgrade/breaking-changes/7-0-0.md +++ b/upgrade/breaking-changes/7-0-0.md @@ -44,7 +44,7 @@ The obsolete `ClearButton` parameter is removed. Use `ShowClearButton` instead. * The obsolete `AutoFitColumns()` method is removed. Use `AutoFitColumnsAsync()` instead. * The obsolete `AutoFitAllColumns()` method is removed. Use `AutoFitAllColumnsAsync()` instead. * When using [grouping and `OnRead`]({%slug components/grid/manual-operations%}#grouping-with-onread), casting `DataSourceResult.Data` to a list of objects (`.Cast()`) is no longer needed. -* [Components in a Grid column `