Skip to content

Commit ceb7fa6

Browse files
authored
kb(Grid): Polish Hierarchy column KB (#2905)
1 parent 43d57d7 commit ceb7fa6

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

Diff for: knowledge-base/grid-customize-hierarchy-expand-column-blazor-grid.md

+44-30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: How to Reorder, Lock or Resize the Hierarchy Expand/Collapse Column in Telerik Blazor Grid
3-
description: Learn how to customize the hierarchy expand/collapse column in Telerik Blazor Grid, including changing its position, locking it, and setting its width and title.
2+
title: Hide, Reorder, Lock or Resize the Grid Hierarchy Expand/Collapse Column
3+
description: Learn how to hide, reorder, lock (freeze), resize, or customize (set a title) the hierarchy expand/collapse column of the Telerik Blazor Grid.
44
type: how-to
5-
page_title: How to Reorder, Lock or Resize the Hierarchy Expand/Collapse Column in Telerik Blazor Grid
5+
page_title: How to Hide, Reorder, Lock or Resize the Grid Hierarchy Expand/Collapse Column
66
slug: grid-kb-customize-hierarchy-expand-column-blazor-grid
77
tags: blazor, grid, hierarchy, expand, collapse, column
88
res_type: kb
9-
ticketid: 1658470,1642380,1638466,1635003,1630238
9+
ticketid: 1658470, 1642380, 1638466, 1635003, 1630238, 1684240
1010
---
1111

1212
## Environment
@@ -28,10 +28,12 @@ This KB article answers the following questions:
2828
* Is it possible to lock/freeze the expand column for a hierarchical grid?
2929
* Can I add a title to the hierarchy expand/collapse column?
3030
* How can I change the width of the expand column in a hierarchical Grid?
31+
* How to hide the Grid hierarchy expand/collapse column?
32+
* How to expand the Grid detail template with a link in the data row, instead of using the plus icon sign?
3133

3234
## Solution
3335

34-
By default, the hierarchy expand/collapse column in the Telerik UI for Blazor [Grid](slug:grid-overview) is not declared in the markup like the other data-bound columns. It renders automatically when a `DetailTemplate` is added to the Grid. At the time of writing (UI for Blazor **6.0.2**), this built-in hierarchy expand/collapse column does not support being locked or other common configurations like managing its position, setting its width, or adding a title.
36+
By default, the hierarchy expand/collapse column in the Telerik UI for Blazor [Grid](slug:grid-overview) is not declared in the markup like the other data-bound columns. It renders automatically when a `DetailTemplate` is added to the Grid. At the time of writing (UI for Blazor **8.1.1**), this built-in hierarchy expand/collapse column does not support being locked or other common configurations like managing its position, setting its width, or adding a title.
3537

3638
>tip Vote for and follow the [feature request for the Grid to support controlling the position of the expand column](https://feedback.telerik.com/blazor/1647135-ability-to-control-the-position-of-the-expand-column-in-a-hierarchical-grid).
3739
@@ -59,38 +61,49 @@ To customize the hierarchy expand/collapse column, follow these steps:
5961
</GridSettings>
6062
<DetailTemplate>
6163
@{
62-
var employee = context as MainModel;
64+
var employee = (MainModel)context;
6365
6466
<TelerikGrid Data="employee.Orders" Pageable="true" PageSize="5">
6567
<GridColumns>
6668
<GridColumn Field="OrderId"></GridColumn>
67-
<GridColumn Field="DealSize"></GridColumn>
69+
<GridColumn Field="DealSize" DisplayFormat="{0:c2}"></GridColumn>
6870
</GridColumns>
6971
</TelerikGrid>
7072
}
7173
</DetailTemplate>
7274
<GridColumns>
7375
<GridColumn Field="@nameof(MainModel.Id)" Width="60px"></GridColumn>
74-
<GridColumn Field="@nameof(MainModel.Name)"></GridColumn>
76+
<GridColumn Field="@nameof(MainModel.Name)">
77+
<Template>
78+
@{
79+
var dataItem = (MainModel)context;
80+
}
81+
<TelerikButton FillMode="@ThemeConstants.Button.FillMode.Link"
82+
ThemeColor="@ThemeConstants.Button.ThemeColor.Success"
83+
OnClick="@( () => OnButtonClick(dataItem) )">@dataItem.Name</TelerikButton>
84+
</Template>
85+
</GridColumn>
7586
<GridColumn Field="@nameof(MainModel.Address)"></GridColumn>
7687
<GridColumn Field="@nameof(MainModel.Email)"></GridColumn>
7788
<GridColumn Width="60px">
7889
<Template>
7990
@{
8091
var dataItem = (MainModel)context;
8192
}
82-
<TelerikButton Icon="@GetButtonIcon(dataItem)" FillMode="@Telerik.Blazor.ThemeConstants.Button.FillMode.Flat" OnClick="@( () => OnButtonClick(dataItem) )" />
93+
<TelerikButton Icon="@GetButtonIcon(dataItem)"
94+
FillMode="@Telerik.Blazor.ThemeConstants.Button.FillMode.Flat"
95+
OnClick="@( () => OnButtonClick(dataItem) )" />
8396
</Template>
8497
</GridColumn>
8598
</GridColumns>
8699
</TelerikGrid>
87100
88101
<style>
89-
/* shrink hierarchy column - do not hide it completely */
102+
/* shrink the hierarchy column - do not hide it completely */
90103
.no-default-hierarchy-column .k-hierarchy-col {
91104
width: 0;
92105
}
93-
/* hide everything from hierarchy column */
106+
/* hide everything in the hierarchy column */
94107
.no-default-hierarchy-column .k-hierarchy-cell * {
95108
display: none;
96109
}
@@ -103,7 +116,7 @@ To customize the hierarchy expand/collapse column, follow these steps:
103116
104117
private async Task OnButtonClick(MainModel dataItem)
105118
{
106-
var gridState = GridRef.GetState();
119+
var gridState = GridRef!.GetState();
107120
108121
if (gridState.ExpandedItems.Contains(dataItem))
109122
{
@@ -119,7 +132,7 @@ To customize the hierarchy expand/collapse column, follow these steps:
119132
120133
private ISvgIcon GetButtonIcon(MainModel dataItem)
121134
{
122-
var gridState = GridRef.GetState();
135+
var gridState = GridRef!.GetState();
123136
124137
return GetButtonIcon(gridState.ExpandedItems.Contains(dataItem));
125138
}
@@ -144,10 +157,10 @@ To customize the hierarchy expand/collapse column, follow these steps:
144157
private List<MainModel> GenerateData()
145158
{
146159
List<MainModel> data = new List<MainModel>();
147-
for (int i = 0; i < 5; i++)
160+
for (int i = 1; i <= 5; i++)
148161
{
149162
MainModel mdl = new MainModel { Id = i, Name = $"Name {i}", Address = $"Address {i}", Email = $"example{i}@mail.com" };
150-
mdl.Orders = Enumerable.Range(1, 15).Select(x => new DetailsModel { OrderId = x, DealSize = x ^ i }).ToList();
163+
mdl.Orders = Enumerable.Range(1, 15).Select(x => new DetailModel { OrderId = x, DealSize = Random.Shared.Next(100, 10000) }).ToList();
151164
data.Add(mdl);
152165
}
153166
return data;
@@ -156,23 +169,24 @@ To customize the hierarchy expand/collapse column, follow these steps:
156169
public class MainModel
157170
{
158171
public int Id { get; set; }
159-
public string Name { get; set; }
160-
public string Address { get; set; }
161-
public string Email { get; set; }
162-
public List<DetailsModel> Orders { get; set; }
172+
public string Name { get; set; } = string.Empty;
173+
public string Address { get; set; } = string.Empty;
174+
public string Email { get; set; } = string.Empty;
175+
public List<DetailModel> Orders { get; set; } = new();
163176
164177
//override the Equals() method of the Grid model to ensure the items are properly expanded in case their references are changed
165-
public override bool Equals(object obj)
178+
public override bool Equals(object? obj)
179+
{
180+
return obj is MainModel && ((MainModel)obj).Id == Id;
181+
}
182+
183+
public override int GetHashCode()
166184
{
167-
if (obj is MainModel)
168-
{
169-
return this.Id == (obj as MainModel).Id;
170-
}
171-
return false;
185+
return base.GetHashCode();
172186
}
173187
}
174188
175-
public class DetailsModel
189+
public class DetailModel
176190
{
177191
public int OrderId { get; set; }
178192
public double DealSize { get; set; }
@@ -182,7 +196,7 @@ To customize the hierarchy expand/collapse column, follow these steps:
182196

183197
## See Also
184198

185-
- [Telerik Blazor Grid - Overview](slug:grid-overview)
186-
- [Telerik Blazor Grid - Column Templates](slug:grid-templates-column)
187-
- [Telerik Blazor Grid - Hierarchy](slug:components/grid/features/hierarchy)
188-
- [Telerik Documentation - Styling and Themes](slug:themes-override)
199+
* [Telerik Blazor Grid - Overview](slug:grid-overview)
200+
* [Telerik Blazor Grid - Column Templates](slug:grid-templates-column)
201+
* [Telerik Blazor Grid - Hierarchy](slug:components/grid/features/hierarchy)
202+
* [Telerik Documentation - Styling and Themes](slug:themes-override)

0 commit comments

Comments
 (0)