Skip to content

docs(TabStrip): add dynamic tabs docs #2889

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
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/tabstrip/active-tab-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Active Tab Index: @ActiveTabIndex
}
````

> The `ActiveTabIndexChanged` event and `ActiveTabIndex` parameter will be deprecated in a future releases. It is recommended to use the [`ActiveTabId`](slug:tabstrip-tabs-collection) parameter with [`ActiveTabIdChanged`](slug:tabstrip-events#activetabidchanged) event instead.

## See Also

* [Live Demo: TabStrip](https://demos.telerik.com/blazor-ui/tabstrip/overview)
Expand Down
34 changes: 34 additions & 0 deletions components/tabstrip/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,47 @@ position: 20

This article explains the events available in the Telerik TabStrip for Blazor:

* [ActiveTabIdChanged](#activetabidchanged)
* [ActiveTabIndexChanged](#activetabindexchanged)

## ActiveTabIdChanged

The `ActiveTabIdChanged` event fires when the user changes the active tab. The event handler receives the new tab ID of type `string` as an argument. This event is designed to work with the new [`ActiveTabId` parameter](slug:tabstrip-tabs-collection).

>caption Handle the tab ID selection changed event

````RAZOR
<TelerikTabStrip ActiveTabIdChanged="@HandleTabIdChange">
<TabStripTab Title="First">
First tab content. Click through the tabs.
</TabStripTab>
<TabStripTab Title="Second">
Second tab content.
</TabStripTab>
<TabStripTab Title="Third">
Third tab content.
</TabStripTab>
</TelerikTabStrip>

@Result

@code {
private string Result { get; set; }
private void HandleTabIdChange(string tabId)
{
Result = $"Current tab ID is {tabId}";
}
}
````

## ActiveTabIndexChanged

The `ActiveTabIndexChanged` event fires when the user changes the tab that is currently shown. The event handler receives the new index as an argument.

If you remove programmatically the currently active tab, when it disposes, the event will fire with index `-1` as there will be no selected tab anymore.

> The `ActiveTabIndexChanged` event and `ActiveTabIndex` parameter will be deprecated in a future releases. It is recommended to use the [`ActiveTabId`](slug:tabstrip-tabs-collection) parameter with [`ActiveTabIdChanged`](slug:tabstrip-events#activetabidchanged) event instead.

>caption Handle the tab selection changed event

````RAZOR
Expand Down Expand Up @@ -81,3 +114,4 @@ If you remove programmatically the currently active tab, when it disposes, the e
## See Also

* [TabStrip Overview](slug:components/tabstrip/overview)
* [Dynamic Tabs](slug:tabstrip-tabs-collection)
9 changes: 7 additions & 2 deletions components/tabstrip/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ The Blazor TabStrip component can persist the content of the tabs. When the user

The Blazor TabStrip allows you to scroll only its tabs. This is useful for scenarios where a lot of tabs are defined. [Read more about the Scrollable Tabs...](slug:tabstrip-scroll-tabs)

## Dynamic Tabs

The Blazor TabStrip component allows you to create TabStrip tabs dynamically. [Read more about the Dynamic Tabs...](slug:tabstrip-tabs-collection)

## Events

The TabStrip fires an [`ActiveTabIndexChanged` event](slug:tabstrip-events) when the user clicks on a tab to select it.
The TabStrip fires an `ActiveTabIndexChanged`and `ActiveTabIdChanged` events when the user clicks on a tab to select it. [Read more about the TabStrip events...](slug:tabstrip-events)

## TabStrip Parameters

Expand All @@ -76,7 +80,8 @@ The TabStrip provides the following features to allow further customization of i

| Parameter | Type | Header 2 |
|------------------|-------|------------------------------------------|
| `ActiveTabIndex` | `int` | The index of the currently shown tab. Supports two-way binding.
| `ActiveTabIndex` | `int` | The index of the currently shown tab. Supports two-way binding. This parameter is marked as obsolete and will be deprecated in future versions. Do not use togother with `ActiveTabId`. |
| `ActiveTabId` | `int` | The index of the currently active tab. If it is not set, the first tab will be active. Do not use it together with `ActiveTabIndex`.|
|`PersistTabContent` | `bool` | Whether to remove the content of inactive tabs from the DOM (if `false`), or just hide it with CSS (if `true`). See [Persist Content](slug:tabstrip-persist-content)
| `Scrollable` | `bool` | Whether the tabs will be scrollable. See [Scrollable Tabs](slug:tabstrip-scroll-tabs)
| `ScrollButtonsPosition` | `TabStripScrollButtonsPosition` enum <br/> (`TabStripScrollButtonsPosition.Split`)| Specifies the position of the buttons when the TabStrip is scrollable.
Expand Down
79 changes: 44 additions & 35 deletions components/tabstrip/tabs-collection.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,75 @@
---
title: Tabs Collection
page_title: TabStrip Tabs Collection
description: Overview of the TabStrip for Blazor.
title: Dynamic Tabs
page_title: TabStrip - Dynamic Tabs
description: Learn how to use the ActiveTabId parameter in the Telerik TabStrip for Blazor to manage dynamic tabs.
slug: tabstrip-tabs-collection
tags: telerik,blazor,tab,strip,tabstrip,collection
tags: telerik,blazor,tabstrip,dynamic tabs
published: True
position: 17
position: 3
---

# TabStrip Tabs Collection
# Dynamic Tabs in TabStrip

In some cases, you might need to declare tabs for objects in a collection. The TabStrip allows you to render its tabs by iterating that collection.
In some cases, you might need to declare tabs for objects in a collection. The Telerik TabStrip allows you to render its tabs by iterating that collection.

This is an alternative approach for configuring the component instead of manually declaring each tab as a separate `TabStripTab` instance inside the `TabStrip` tag.
The Telerik Tabstrip supports effective management of dynamic tabs through the `ActiveTabId` parameter and the [`ActiveTabIdChanged`](slug:tabstrip-events#activetabidchanged) event. These features allow users to specify or track the active tab using its unique ID, making it easier to work with dynamic tab scenarios.

>tip If you render components in the tabs created in a `foreach` loop, you may want to set their `@key` parameter to unique values, in order to ensure they will re-render. If you do not, the framework will render one instance of your custom component for all tabs and will only set its parameters, it will not initialize anew (`OnInitialized` will not fire a second time, only `OnParametersSet` will).
## ActiveTabId Parameter

>caption Extract information for the currently selected tab from your model. Alter the model to affect the TabStrip. Create tabs dynamically based on external data.
The `ActiveTabId` parameter allows you to manage the active tab by its ID. It supports two-way binding, allowing seamless updates between the component and the application state.

You can find another example with some more details in the following sample project: [Dynamic Tabs](https://github.com/telerik/blazor-ui/tree/master/tabstrip/DynamicTabs).
To deactivate all tabs, set the ActiveTabId parameter to `string.Empty`.

````RAZOR
@result
>caption Using the `ActiveTabId` parameter to manage dynamic tabs

<TelerikTabStrip ActiveTabIndexChanged="@TabChangedHandler">
````RAZOR
<TelerikTabStrip @bind-ActiveTabId="@ActiveTabId">
@{
foreach (MyTabModel item in tabs)
foreach (var tab in Tabs)
{
<TabStripTab Title="@item.Title" Disabled="@item.Disabled" @key="@item">
Content for tab @item.Title
<TabStripTab @key="tab.Id" Title="@tab.Title" Visible="@tab.Visible" Disabled="@tab.Disabled">
<HeaderTemplate>
<span>@tab.Title</span>
</HeaderTemplate>
<Content>
@if (tab.Id == "home")
{
<p>Welcome back! Check out the latest updates and news here.</p>
}
else if (tab.Id == "profile")
{
<p>Update your personal information and preferences in this section.</p>
}
else if (tab.Id == "settings")
{
<p>Customize your experience by adjusting your settings here.</p>
}
</Content>
</TabStripTab>
}
}
</TelerikTabStrip>

<TelerikButton OnClick="@( () => tabs[1].Disabled = !tabs[1].Disabled )">Toggle the Disabled state of the second tab</TelerikButton>

@code {
MarkupString result { get; set; }
void TabChangedHandler(int newIndex)
{
string tempResult = $"current tab {newIndex} selected on {DateTime.Now}";
MyTabModel currTab = tabs[newIndex];
tempResult += $"<br />the new tab has a title {currTab.Title}";
result = new MarkupString(tempResult);
}
private string ActiveTabId { get; set; }

List<MyTabModel> tabs = new List<MyTabModel>()
{
new MyTabModel { Title = "One" },
new MyTabModel { Title = "Two", Disabled = true },
new MyTabModel { Title = "Three" }
};
private List<Tab> Tabs { get; set; } = new List<Tab>
{
new Tab { Id = "home", Title = "🏠 Home", Visible = true, Disabled = false },
new Tab { Id = "profile", Title = "👤 Profile", Visible = true, Disabled = false },
new Tab { Id = "settings", Title = "⚙️ Settings", Visible = true, Disabled = false }
};

public class MyTabModel
public class Tab
{
public string Id { get; set; }
public string Title { get; set; }
public bool Visible { get; set; }
public bool Disabled { get; set; }
}
}
````

## See Also

* [Live Demo: TabStrip](https://demos.telerik.com/blazor-ui/tabstrip/overview)
* [TabStrip Events](slug:tabstrip-events)
Loading