|
| 1 | +--- |
| 2 | +title: Render All TabStrip Tabs Initially |
| 3 | +description: Learn how to initialize, load and render all tabs on page load in the Telerik TabStrip for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: How to Render All TabStrip Tabs By Default |
| 6 | +slug: tabstrip-kb-load-all-tabs |
| 7 | +tags: telerik, blazor, tabstrip |
| 8 | +ticketid: 1673750, 1654667 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>TabStrip for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB answers the following questions: |
| 26 | + |
| 27 | +* How to preload the contents of all TabStrip tabs and keep it rendered in the DOM at all times? |
| 28 | +* How to load and render all the content of a Telerik TabStrip for Blazor? |
| 29 | +* How to access components in TabStrip tabs that haven't been opened by the user? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +Even when [`PersistTabContent="true"`]({%slug tabstrip-persist-content%}), the TabStrip initializes and renders each tab container for the first time only after the user clicks on the respective tab to activate it. This improves the app performance. |
| 34 | + |
| 35 | +In scenarios where all TabStrip tabs must render initially and be in the DOM at all times, use the following approach: |
| 36 | + |
| 37 | +1. Move the tab content outside the TabStrip. Use one HTML `<div>` to hold the contents of each tab. |
| 38 | +1. Hide the TabStrip tab container elements (`<div class="k-tabstrip-content">`) with a `display:none` CSS style. |
| 39 | +1. Toggle the `display` styles of the HTML `<div>`s in the [TabStrip `ActiveTabIndexChanged` event]({%slug tabstrip-events%}#activetabindexchanged). |
| 40 | + |
| 41 | +>caption Render all TabStrip tabs initially |
| 42 | +
|
| 43 | +````RAZOR |
| 44 | +<TelerikTabStrip @bind-ActiveTabIndex="@ActiveTabIndex" |
| 45 | + Class="empty-tabstrip"> |
| 46 | + <TabStripTab Title="First Tab"> </TabStripTab> |
| 47 | + <TabStripTab Title="Second Tab"> </TabStripTab> |
| 48 | + <TabStripTab Title="Third Tab"> </TabStripTab> |
| 49 | +</TelerikTabStrip> |
| 50 | +
|
| 51 | +<div class="tabstrip-containers k-tabstrip"> |
| 52 | + <div class="@( $"k-tabstrip-content {GetTabActive(0)}" )"> |
| 53 | + First Tab Content |
| 54 | + </div> |
| 55 | + <div class="@( $"k-tabstrip-content {GetTabActive(1)}" )"> |
| 56 | + Second Tab Content |
| 57 | + </div> |
| 58 | + <div class="@( $"k-tabstrip-content {GetTabActive(2)}" )"> |
| 59 | + Third Tab Content |
| 60 | + </div> |
| 61 | +</div> |
| 62 | +
|
| 63 | +<style> |
| 64 | + /* hide built-in tab containers */ |
| 65 | + .empty-tabstrip .k-tabstrip-content { |
| 66 | + display: none !important; |
| 67 | + } |
| 68 | + /* adjust tab borders */ |
| 69 | + .tabstrip-containers .k-tabstrip-content { |
| 70 | + border-top-width: 0; |
| 71 | + } |
| 72 | +</style> |
| 73 | +
|
| 74 | +@code { |
| 75 | + public int ActiveTabIndex { get; set; } |
| 76 | +
|
| 77 | + private string GetTabActive(int index) |
| 78 | + { |
| 79 | + return ActiveTabIndex == index ? "k-active" : string.Empty; |
| 80 | + } |
| 81 | +} |
| 82 | +```` |
| 83 | + |
| 84 | +## See Also |
| 85 | + |
| 86 | +* [Persist TabStrip Tab Content]({%slug tabstrip-persist-content%}) |
| 87 | +* [TabStrip Events]({%slug tabstrip-events%}) |
0 commit comments