Skip to content

Commit 5142258

Browse files
docs(tabStrip): selectByIndex KB
1 parent 0b3ebc5 commit 5142258

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Set active tab by index
3+
description: how to select or activate a tab by index
4+
type: how-to
5+
page_title: Select tab by index
6+
slug: tabstrip-activate-by-index
7+
position:
8+
tags:
9+
ticketid: 1422293
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>TabStrip for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
I have a TabStrip that renders tabs dynamically based on a list. Is there a way to set the active tab based on an index or a key?
26+
27+
## Solution
28+
The `SetActiveTab` method of the TabStrip requires a reference to the tab. So, you can have a list of references to the tabs and pass the desired item from the list to the method.
29+
30+
````CSHTML
31+
@using Telerik.Blazor.Components.TabStrip
32+
@using Telerik.Blazor.Components.Button
33+
@using Telerik.Blazor.Components.NumericTextBox
34+
35+
<TelerikNumericTextBox @bind-Value="@tabToSelect" Min="0" Max="@tabsCount" Step="1"></TelerikNumericTextBox>
36+
<TelerikButton OnClick="@ActivateChosenTab">Activate the tab with the chosen index</TelerikButton>
37+
38+
<TelerikTabStrip @ref="myTabStrip">
39+
@{
40+
foreach (int item in tabTitles)
41+
{
42+
<TelerikTab Title="@( "Tab " + item )" @ref="@theTabsRefs[item]">content for tab @item </TelerikTab>
43+
}
44+
}
45+
</TelerikTabStrip>
46+
47+
@code {
48+
Telerik.Blazor.Components.TabStrip.TelerikTabStrip myTabStrip;
49+
50+
void ActivateChosenTab()
51+
{
52+
if(tabToSelect > theTabsRefs.Count) { return; } // avoid errors in a preferred way, this is just an example
53+
54+
myTabStrip.SetActiveTab(theTabsRefs[tabToSelect]);
55+
}
56+
57+
int tabsCount = 5; // used for data generation and max for the numeric textbox
58+
59+
int tabToSelect { get; set; } = 0; // used for binding the desired index in this example
60+
61+
List<int> tabTitles = new List<int>(); // you may use your own model here, int is used for simplicity because we need an index for the tabs
62+
63+
List<Telerik.Blazor.Components.TabStrip.TelerikTab> theTabsRefs = new List<Telerik.Blazor.Components.TabStrip.TelerikTab>();
64+
65+
protected override void OnInit()
66+
{
67+
base.OnInit();
68+
69+
for (int i = 0; i < tabsCount; i++)
70+
{
71+
theTabsRefs.Add(new TelerikTab()); // you must initialize the collection so references can be put in it
72+
tabTitles.Add(i); // populate the list that generates the tabs
73+
}
74+
}
75+
}
76+
````
77+

0 commit comments

Comments
 (0)