You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: components/grid/templates/column.md
+99-5Lines changed: 99 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,26 @@ position: 5
10
10
11
11
# Column Template
12
12
13
-
By default, each Grid cell renders the value of the respective column `Field` of the current data item (row). You can change this behavior by using the `Template` of the column and adding your own content or logic.
13
+
By default, each Grid cell renders the value of the respective column `Field` of the current data item (row). You can change this behavior by using a column `Template` and adding your own content or logic.
14
14
15
-
The column template (the `<Template>` tag under the column definition) is what the Grid uses to show the "view" representation of the cell. This also includes a column that is marked as `Editable="false"` and is in edit mode.
15
+
## Basics
16
+
17
+
To define a column template, use a `<Template>` tag inside the `<GridColumn>` tag. The Grid uses the defined `Template` to show the "view" representation of all cells in that column. This also includes cells from [columns that are marked as `Editable="false"`]({%slug components/grid/columns/bound%}#data-operations), while the cells' parent row is in [inline edit mode]({%slug components/grid/editing/inline%}).
18
+
19
+
Visual Studio tends to autocomplete the `<Template>` tag with a lowercase `t` which breaks the template logic and does not allow you to access the `context`. Ensure the `Template` tag uses a capital `T`.
20
+
21
+
### Template Context
22
+
23
+
The Grid column template exposes a `context` variable, which is the respective item from the Grid data collection. Cast the `context` to your Grid model type in order to access and use its properties. [Rename the `context` variable when using nested templates, for example a Grid column `Template` inside a Grid `DetailTemplate`]({%slug nest-renderfragment%}).
16
24
17
25
>tip If you only want to format numbers, dates, enums, you can do so with the [DisplayFormat feature]({%slug grid-columns-displayformat%}) without the need to declare a template.
18
26
27
+
## Example
28
+
19
29
The example below shows how to:
20
30
21
-
*Set the `Template` (make sure to use the tag with a capital `T`. The Visual Studio autocomplete tends to use the lowercase `t` which breaks the template logic and does not allow you to access the context).
22
-
*Access the template `context`, which is a data item object from the Grid `Data`. You need to cast the `context` to access the data item properties.
31
+
*Define a Grid column `Template`.
32
+
*Cast and access the template `context`.
23
33
* Render HTML or nest components in the column template.
24
34
* Use inline or multi-line template.
25
35
@@ -86,7 +96,91 @@ The example below shows how to:
86
96
}
87
97
````
88
98
89
-
>tip The above example renders read-only checkboxes to display boolean values. It is possible to [use checkboxes in display mode and directly change the underlying data source values]({%slug grid-kb-checkbox-editing%}). This can make boolean value editing faster, because the Grid doesn't go into edit mode.
99
+
>tip The above example renders read-only checkboxes to display boolean values. You can also [use checkboxes in display mode and directly change the underlying data source values]({%slug grid-kb-checkbox-editing%}). This can make boolean value editing faster, because the Grid doesn't go into edit mode.
100
+
101
+
## Using Components in Grid Column Templates
102
+
103
+
The Grid optimizes the UI renders after data operations. If you are using child components inside Grid column templates, <ahref="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/element-component-model-relationships"target="_blank">set the `@key` attribute to these components to ensure that they always show the correct values and content after filtering, paging, and sorting</a>.
104
+
105
+
>caption Seting @key to child components inside a Grid column template
106
+
<div class="skip-repl"></div>
107
+
````Home.razor
108
+
@using YourAppName.Data
109
+
110
+
<TelerikGrid Data="@GridData"
111
+
TItem="@SampleModel"
112
+
FilterMode="GridFilterMode.FilterRow"
113
+
Pageable="true"
114
+
PageSize="5"
115
+
Sortable="true">
116
+
<GridColumns>
117
+
<GridColumn Field="@nameof(SampleModel.Name)" Title="Template with Key">
Copy file name to clipboardExpand all lines: upgrade/breaking-changes/7-0-0.md
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,40 @@ The obsolete `ClearButton` parameter is removed. Use `ShowClearButton` instead.
44
44
* The obsolete `AutoFitColumns()` method is removed. Use `AutoFitColumnsAsync()` instead.
45
45
* The obsolete `AutoFitAllColumns()` method is removed. Use `AutoFitAllColumnsAsync()` instead.
46
46
* When using [grouping and `OnRead`]({%slug components/grid/manual-operations%}#grouping-with-onread), casting `DataSourceResult.Data` to a list of objects (`.Cast<object>()`) is no longer needed.
47
+
*[Components in a Grid column `<Template>` require a `@key`]({%slug grid-templates-column%}#using-components-in-grid-column-templates) in order to display correct values after data operations like sorting, filtering, paging, and others.
48
+
49
+
>caption Using custom components in Grid column templates up to version 6.2.0 and after version 7.0.0
0 commit comments