Skip to content

Commit f757b29

Browse files
github-actions[bot]KB Botxristianstefanov
authored
Merge new-kb-grid-expand-collapse-multilevel-hierarchy-497031dce66247dd944327c8e21ab5ca-2740 into production (#2781)
* Added new kb article grid-expand-collapse-multilevel-hierarchy * chore(Grid): apply suggestions --------- Co-authored-by: KB Bot <kb-bot@telerik.com> Co-authored-by: Hristian Stefanov <Hristian.Stefanov@progress.com>
1 parent 06b0299 commit f757b29

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: Expand and Collapse All Rows in a Multi-Level Hierarchical Grid
3+
description: Learn how to implement an "Expand/Collapse All" option for a multi-level Hierarchical Grid in Blazor applications.
4+
type: how-to
5+
page_title: How to Implement Expand/Collapse All for Multi-Level Hierarchical Grid in Blazor
6+
slug: grid-kb-expand-collapse-multilevel-hierarchy
7+
tags: blazor, grid, hierarchy, state
8+
res_type: kb
9+
ticketid: 1675753
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>Grid for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
## Description
23+
24+
In a multi-level Hierarchical Grid structure, it is common to require functionality that allows users to expand or collapse all parent, child, and grandchild rows with a single action. This article demonstrates how to implement an "Expand/Collapse All" button for a Blazor application that controls the expansion state of all rows in a three-level hierarchical grid structure: Parent Grid -> Child Grid -> Grand Child Grid.
25+
26+
## Solution
27+
28+
To control the expansion and collapse of all rows in a multi-level Hierarchical Grid, utilize the [Grid State](slug://grid-state).
29+
30+
1. Use the [`SetStateAsync` method](slug://grid-state#methods) for the parent Grid to control the expansion and collapse of its rows.
31+
2. Use the [`OnStateInit` event](slug://grid-state#onstateinit) of the child Grids to expand their rows by default when the parent Grid rows expand.
32+
3. If some child Grids are already visible and their `OnStateInit` event has been fired, use `SetStateAsync` through their [dynamic references](slug://common-kb-dynamic-refs) to control their expansion state.
33+
34+
````RAZOR
35+
<TelerikGrid @ref="@GridRef"
36+
Data="@GridData">
37+
<GridToolBarTemplate>
38+
<GridCommandButton OnClick="@OnExpandAllClick">Expand All</GridCommandButton>
39+
<GridCommandButton OnClick="@OnCollapseAllClick">Collapse All</GridCommandButton>
40+
</GridToolBarTemplate>
41+
<GridColumns>
42+
<GridColumn Field="@nameof(SampleModel.Name)" />
43+
</GridColumns>
44+
<DetailTemplate Context="masterItem">
45+
<TelerikGrid Data="@ChildGridData.Where(x => x.ParentId == masterItem.Id)"
46+
OnStateInit="@( (GridStateEventArgs<SampleModel> args) => OnChildGridStateInit(args, masterItem.Id))">
47+
<GridColumns>
48+
<GridColumn Field="@nameof(SampleModel.Name)" />
49+
</GridColumns>
50+
<DetailTemplate Context="childItem">
51+
<TelerikGrid Data="@GrandChildGridData.Where(x => x.ParentId == childItem.Id)">
52+
<GridColumns>
53+
<GridColumn Field="@nameof(SampleModel.Name)" />
54+
</GridColumns>
55+
</TelerikGrid>
56+
</DetailTemplate>
57+
</TelerikGrid>
58+
</DetailTemplate>
59+
</TelerikGrid>
60+
61+
@code {
62+
private TelerikGrid<SampleModel>? GridRef { get; set; }
63+
private List<SampleModel> GridData { get; set; } = new();
64+
private List<SampleModel> ChildGridData { get; set; } = new();
65+
private List<SampleModel> GrandChildGridData { get; set; } = new();
66+
67+
private async Task OnCollapseAllClick()
68+
{
69+
var gridState = GridRef!.GetState();
70+
71+
gridState.ExpandedItems = new List<SampleModel>();
72+
73+
await GridRef.SetStateAsync(gridState);
74+
}
75+
76+
private async Task OnExpandAllClick()
77+
{
78+
var gridState = GridRef!.GetState();
79+
80+
gridState.ExpandedItems = new List<SampleModel>(GridData);
81+
82+
await GridRef.SetStateAsync(gridState);
83+
}
84+
85+
private void OnChildGridStateInit(GridStateEventArgs<SampleModel> args, int masterId)
86+
{
87+
args.GridState.ExpandedItems = ChildGridData.Where(x => x.ParentId == masterId).ToList();
88+
}
89+
90+
protected override void OnInitialized()
91+
{
92+
for (int i = 1; i <= 2; i++)
93+
{
94+
GridData.Add(new SampleModel()
95+
{
96+
Id = i,
97+
Name = $"Name {i}"
98+
});
99+
100+
for (int j = 1; j <= 2; j++)
101+
{
102+
ChildGridData.Add(new SampleModel()
103+
{
104+
Id = 100 * i + j,
105+
ParentId = i,
106+
Name = $"Name {100 * i + j}"
107+
});
108+
109+
for (int k = 1; k <= 2; k++)
110+
{
111+
GrandChildGridData.Add(new SampleModel()
112+
{
113+
Id = 1000 * i + 100 * j + k,
114+
ParentId = 100 * i + j,
115+
Name = $"Name {1000 * i + 100 * j + k}"
116+
});
117+
118+
}
119+
}
120+
}
121+
}
122+
123+
public class SampleModel
124+
{
125+
public int Id { get; set; }
126+
public int? ParentId { get; set; }
127+
public string Name { get; set; } = string.Empty;
128+
}
129+
}
130+
````
131+
132+
## See Also
133+
134+
* [Grid State Documentation](slug://grid-state)
135+
* [Nesting Render Fragments in Blazor](slug://nest-renderfragment)
136+
* [Dynamic References in Blazor](slug://common-kb-dynamic-refs)

0 commit comments

Comments
 (0)