-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article grid-expand-collapse-multilevel-hierarchy #2740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xristianstefanov
merged 2 commits into
master
from
new-kb-grid-expand-collapse-multilevel-hierarchy-497031dce66247dd944327c8e21ab5ca
Feb 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
knowledge-base/grid-expand-collapse-multilevel-hierarchy.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| --- | ||
| title: Expand and Collapse All Rows in a Multi-Level Hierarchical Grid | ||
| description: Learn how to implement an "Expand/Collapse All" option for a multi-level Hierarchical Grid in Blazor applications. | ||
| type: how-to | ||
| page_title: How to Implement Expand/Collapse All for Multi-Level Hierarchical Grid in Blazor | ||
| slug: grid-kb-expand-collapse-multilevel-hierarchy | ||
| tags: grid, hierarchical, grid, expand, collapse, blazor, state,multilevel | ||
| res_type: kb | ||
| ticketid: 1675753 | ||
| --- | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| ## Solution | ||
|
|
||
| To control the expansion and collapse of all rows in a multi-level Hierarchical Grid, utilize the [Grid State](slug://grid-state). | ||
|
|
||
| 1. Use the [`SetStateAsync` method](slug://grid-state#methods) for the parent Grid to control the expansion and collapse of its rows. | ||
| 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. | ||
| 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. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ````RAZOR | ||
| <TelerikGrid @ref="@GridRef" | ||
| Data="@GridData"> | ||
| <GridToolBarTemplate> | ||
| <GridCommandButton OnClick="@OnExpandAllClick">Expand All</GridCommandButton> | ||
| <GridCommandButton OnClick="@OnCollapseAllClick">Collapse All</GridCommandButton> | ||
| </GridToolBarTemplate> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(SampleModel.Name)" /> | ||
| </GridColumns> | ||
| <DetailTemplate Context="masterItem"> | ||
| <TelerikGrid Data="@ChildGridData.Where(x => x.ParentId == masterItem.Id)" | ||
| OnStateInit="@( (GridStateEventArgs<SampleModel> args) => OnChildGridStateInit(args, masterItem.Id))"> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(SampleModel.Name)" /> | ||
| </GridColumns> | ||
| <DetailTemplate Context="childItem"> | ||
| <TelerikGrid Data="@GrandChildGridData.Where(x => x.ParentId == childItem.Id)"> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(SampleModel.Name)" /> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
| </DetailTemplate> | ||
| </TelerikGrid> | ||
| </DetailTemplate> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private TelerikGrid<SampleModel>? GridRef { get; set; } | ||
| private List<SampleModel> GridData { get; set; } = new(); | ||
| private List<SampleModel> ChildGridData { get; set; } = new(); | ||
| private List<SampleModel> GrandChildGridData { get; set; } = new(); | ||
|
|
||
| private async Task OnCollapseAllClick() | ||
| { | ||
| var gridState = GridRef!.GetState(); | ||
|
|
||
| gridState.ExpandedItems = new List<SampleModel>(); | ||
|
|
||
| await GridRef.SetStateAsync(gridState); | ||
| } | ||
|
|
||
| private async Task OnExpandAllClick() | ||
| { | ||
| var gridState = GridRef!.GetState(); | ||
|
|
||
| gridState.ExpandedItems = new List<SampleModel>(GridData); | ||
|
|
||
| await GridRef.SetStateAsync(gridState); | ||
| } | ||
|
|
||
| private void OnChildGridStateInit(GridStateEventArgs<SampleModel> args, int masterId) | ||
| { | ||
| args.GridState.ExpandedItems = ChildGridData.Where(x => x.ParentId == masterId).ToList(); | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| for (int i = 1; i <= 2; i++) | ||
| { | ||
| GridData.Add(new SampleModel() | ||
| { | ||
| Id = i, | ||
| Name = $"Name {i}" | ||
| }); | ||
|
|
||
| for (int j = 1; j <= 2; j++) | ||
| { | ||
| ChildGridData.Add(new SampleModel() | ||
| { | ||
| Id = 100 * i + j, | ||
| ParentId = i, | ||
| Name = $"Name {100 * i + j}" | ||
| }); | ||
|
|
||
| for (int k = 1; k <= 2; k++) | ||
| { | ||
| GrandChildGridData.Add(new SampleModel() | ||
| { | ||
| Id = 1000 * i + 100 * j + k, | ||
| ParentId = 100 * i + j, | ||
| Name = $"Name {1000 * i + 100 * j + k}" | ||
| }); | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class SampleModel | ||
| { | ||
| public int Id { get; set; } | ||
| public int? ParentId { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| * [Grid State Documentation](slug://grid-state) | ||
| * [Nesting Render Fragments in Blazor](slug://nest-renderfragment) | ||
| * [Dynamic References in Blazor](slug://common-kb-dynamic-refs) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.