|
| 1 | +--- |
| 2 | +title: Pre-Expanding PanelBar Items in Blazor on Initialization |
| 3 | +description: Learn how to pre-expand PanelBar items with child items when initializing the component in a Blazor application. |
| 4 | +type: how-to |
| 5 | +page_title: How to Pre-Expand PanelBar Items with Child Items in Blazor |
| 6 | +slug: panelbar-kb-pre-expand-items |
| 7 | +tags: panelbar, blazor, expandeditems |
| 8 | +res_type: kb |
| 9 | +ticketid: 1673208 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>PanelBar for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +In a Blazor application, it is often required to have specific PanelBar items pre-expanded when the component is initially loaded. This might include PanelBar items that contain child items. Achieving this behavior enhances user navigation and usability by directly showcasing important or frequently accessed items. |
| 26 | + |
| 27 | +This knowledge base article also answers the following questions: |
| 28 | + |
| 29 | +- How can I set PanelBar items to be expanded on initial load in Blazor? |
| 30 | +- What is the method to pre-expand PanelBar items with child items? |
| 31 | +- How to use the `ExpandedItems` collection for PanelBar in Blazor? |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +To pre-expand PanelBar items that contain child items upon initialization, modify the `ExpandedItems` collection within the [`OnInitialized()` method](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.componentbase.oninitialized?view=aspnetcore-9.0). The `ExpandedItems` property is responsible for controlling which PanelBar items are expanded or collapsed. By specifying the items that should be initially expanded in this collection, the desired items will be expanded when the PanelBar is first rendered. |
| 36 | + |
| 37 | +Here is an example illustrating how to implement this approach: |
| 38 | + |
| 39 | +````RAZOR |
| 40 | +<div style="width: 30%;"> |
| 41 | + <TelerikPanelBar Data="@Items" |
| 42 | + @bind-ExpandedItems="@ExpandedItems"> |
| 43 | + </TelerikPanelBar> |
| 44 | +</div> |
| 45 | +
|
| 46 | +@code { |
| 47 | + private List<PanelBarItem> Items { get; set; } |
| 48 | + private IEnumerable<object> ExpandedItems { get; set; } = Enumerable.Empty<object>(); |
| 49 | +
|
| 50 | + protected override void OnInitialized() |
| 51 | + { |
| 52 | + Items = LoadFlatData(); |
| 53 | + // Specify the items to be expanded by adding them to the ExpandedItems collection |
| 54 | + ExpandedItems = new List<object>() { Items[0], Items[1] }; |
| 55 | + base.OnInitialized(); |
| 56 | + } |
| 57 | +
|
| 58 | + private List<PanelBarItem> LoadFlatData() |
| 59 | + { |
| 60 | + List<PanelBarItem> items = new List<PanelBarItem>(); |
| 61 | +
|
| 62 | + // Define your PanelBar items and their hierarchy here |
| 63 | + items.Add(new PanelBarItem |
| 64 | + { |
| 65 | + Id = 1, |
| 66 | + Text = "Parent 1", |
| 67 | + ParentId = null, |
| 68 | + HasChildren = true |
| 69 | + }); |
| 70 | +
|
| 71 | + items.Add(new PanelBarItem |
| 72 | + { |
| 73 | + Id = 2, |
| 74 | + Text = "Parent 2", |
| 75 | + ParentId = null, |
| 76 | + HasChildren = true, |
| 77 | + }); |
| 78 | +
|
| 79 | + items.Add(new PanelBarItem |
| 80 | + { |
| 81 | + Id = 3, |
| 82 | + Text = "Child 1 of Parent 2", |
| 83 | + ParentId = 2, |
| 84 | + HasChildren = false |
| 85 | + }); |
| 86 | +
|
| 87 | + items.Add(new PanelBarItem |
| 88 | + { |
| 89 | + Id = 4, |
| 90 | + Text = "Child 2 of Parent 2", |
| 91 | + ParentId = 2, |
| 92 | + HasChildren = false |
| 93 | + }); |
| 94 | +
|
| 95 | + items.Add(new PanelBarItem |
| 96 | + { |
| 97 | + Id = 5, |
| 98 | + Text = "Child 1 of Parent 1", |
| 99 | + ParentId = 1, |
| 100 | + HasChildren = false |
| 101 | + }); |
| 102 | +
|
| 103 | + return items; |
| 104 | + } |
| 105 | + public class PanelBarItem |
| 106 | + { |
| 107 | + public int Id { get; set; } |
| 108 | + public string Text { get; set; } |
| 109 | + public int? ParentId { get; set; } |
| 110 | + public bool HasChildren { get; set; } |
| 111 | + } |
| 112 | +} |
| 113 | +```` |
| 114 | + |
| 115 | +## See Also |
| 116 | + |
| 117 | +- [PanelBar Expanded Items]({%slug panelbar-expand-items%}) |
| 118 | +- [PanelBar Overview]({%slug panelbar-overview%}) |
0 commit comments