|
| 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: grid, hierarchical, grid, expand, collapse, blazor, state,multilevel |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675753 |
| 10 | +--- |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +## Solution |
| 17 | + |
| 18 | +To control the expansion and collapse of all rows in a multi-level Hierarchical Grid, utilize the [Grid State](slug://grid-state). |
| 19 | + |
| 20 | +1. Use the [`SetStateAsync` method](slug://grid-state#methods) for the parent Grid to control the expansion and collapse of its rows. |
| 21 | +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. |
| 22 | +3. If some child Grids are already visible and their `OnStateInit` event has been fired, use `SetStateAsync` for these child Grids to control their expansion state. |
| 23 | + |
| 24 | +````RAZOR |
| 25 | +<TelerikGrid @ref="@GridRef" |
| 26 | + Data="@GridData"> |
| 27 | + <GridToolBarTemplate> |
| 28 | + <GridCommandButton OnClick="@OnExpandAllClick">Expand All</GridCommandButton> |
| 29 | + <GridCommandButton OnClick="@OnCollapseAllClick">Collapse All</GridCommandButton> |
| 30 | + </GridToolBarTemplate> |
| 31 | + <GridColumns> |
| 32 | + <GridColumn Field="@nameof(SampleModel.Name)" /> |
| 33 | + </GridColumns> |
| 34 | + <DetailTemplate Context="masterItem"> |
| 35 | + <TelerikGrid Data="@ChildGridData.Where(x => x.ParentId == masterItem.Id)" |
| 36 | + OnStateInit="@( (GridStateEventArgs<SampleModel> args) => OnChildGridStateInit(args, masterItem.Id))"> |
| 37 | + <GridColumns> |
| 38 | + <GridColumn Field="@nameof(SampleModel.Name)" /> |
| 39 | + </GridColumns> |
| 40 | + <DetailTemplate Context="childItem"> |
| 41 | + <TelerikGrid Data="@GrandChildGridData.Where(x => x.ParentId == childItem.Id)"> |
| 42 | + <GridColumns> |
| 43 | + <GridColumn Field="@nameof(SampleModel.Name)" /> |
| 44 | + </GridColumns> |
| 45 | + </TelerikGrid> |
| 46 | + </DetailTemplate> |
| 47 | + </TelerikGrid> |
| 48 | + </DetailTemplate> |
| 49 | +</TelerikGrid> |
| 50 | +
|
| 51 | +@code { |
| 52 | + private TelerikGrid<SampleModel>? GridRef { get; set; } |
| 53 | + private List<SampleModel> GridData { get; set; } = new(); |
| 54 | + private List<SampleModel> ChildGridData { get; set; } = new(); |
| 55 | + private List<SampleModel> GrandChildGridData { get; set; } = new(); |
| 56 | +
|
| 57 | + private async Task OnCollapseAllClick() |
| 58 | + { |
| 59 | + var gridState = GridRef!.GetState(); |
| 60 | +
|
| 61 | + gridState.ExpandedItems = new List<SampleModel>(); |
| 62 | +
|
| 63 | + await GridRef.SetStateAsync(gridState); |
| 64 | + } |
| 65 | +
|
| 66 | + private async Task OnExpandAllClick() |
| 67 | + { |
| 68 | + var gridState = GridRef!.GetState(); |
| 69 | +
|
| 70 | + gridState.ExpandedItems = new List<SampleModel>(GridData); |
| 71 | +
|
| 72 | + await GridRef.SetStateAsync(gridState); |
| 73 | + } |
| 74 | +
|
| 75 | + private void OnChildGridStateInit(GridStateEventArgs<SampleModel> args, int masterId) |
| 76 | + { |
| 77 | + args.GridState.ExpandedItems = ChildGridData.Where(x => x.ParentId == masterId).ToList(); |
| 78 | + } |
| 79 | +
|
| 80 | + protected override void OnInitialized() |
| 81 | + { |
| 82 | + for (int i = 1; i <= 2; i++) |
| 83 | + { |
| 84 | + GridData.Add(new SampleModel() |
| 85 | + { |
| 86 | + Id = i, |
| 87 | + Name = $"Name {i}" |
| 88 | + }); |
| 89 | +
|
| 90 | + for (int j = 1; j <= 2; j++) |
| 91 | + { |
| 92 | + ChildGridData.Add(new SampleModel() |
| 93 | + { |
| 94 | + Id = 100 * i + j, |
| 95 | + ParentId = i, |
| 96 | + Name = $"Name {100 * i + j}" |
| 97 | + }); |
| 98 | +
|
| 99 | + for (int k = 1; k <= 2; k++) |
| 100 | + { |
| 101 | + GrandChildGridData.Add(new SampleModel() |
| 102 | + { |
| 103 | + Id = 1000 * i + 100 * j + k, |
| 104 | + ParentId = 100 * i + j, |
| 105 | + Name = $"Name {1000 * i + 100 * j + k}" |
| 106 | + }); |
| 107 | +
|
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + public class SampleModel |
| 114 | + { |
| 115 | + public int Id { get; set; } |
| 116 | + public int? ParentId { get; set; } |
| 117 | + public string Name { get; set; } = string.Empty; |
| 118 | + } |
| 119 | +} |
| 120 | +```` |
| 121 | + |
| 122 | +## See Also |
| 123 | + |
| 124 | +* [Grid State Documentation](slug://grid-state) |
| 125 | +* [Nesting Render Fragments in Blazor](slug://nest-renderfragment) |
| 126 | +* [Dynamic References in Blazor](slug://common-kb-dynamic-refs) |
0 commit comments