Cannot activate newly added tab

Hi i'm trying to activate newly added tab that added programmatically. New tab is added but when i try to activate the new tab it doesn't work.

I add a new tab with the following code and handle the OnTabAdded event. 

public void NodeSelectedHandler(NodeSelectEventArgs args)
{
    var NavId = args.NodeData.Id;
    if (!string.IsNullOrWhiteSpace(NavId))
    {
        var tabItem = mainTab.GetTabItemById(NavId);
        if (tabItem == null )
        {
            var navigationItem = NavigationItems.Single(x => x.Id == NavId);
            if (navigationItem.HasContent)
            {
                var newTabIndex = mainTab.Items.Count;
                var newTab = new TabItem
                    {
                        ID = navigationItem.Id,
                        Header = new TabHeader { Text = navigationItem.DisplayName },
                        ContentTemplate = navigationItem.Content,
                        TabIndex = newTabIndex
                    };


                mainTab.AddTab(new List<TabItem> { newTab }, newTabIndex);
            }
        }
        else
        {
            mainTab.SelectAsync(tabItem.TabIndex);
        }
    }
}

SfTab? mainTab;
public void OnTabAdded(AddEventArgs args)
{
    mainTab.SelectedItem = args.AddedItems[0].TabIndex;
}


3 Replies

RR Ram Raju Elaiyaperumal Syncfusion Team June 17, 2024 04:36 PM UTC

Hi Steven


Based on your query we found that you requirement is to enable the newly added tab, You can achieve your requirement with selectedItem property, and passing the newly added tab Index to it,

Here is how you can achieve it


<SfTab @ref="@TabInstance" CssClass="e-sheet-tab"

       Items="@SheetTabs" SelectedItem="@newTabIndex">

</SfTab>

 

 

@code {

 

   

    private int newTabIndex { get; set; } = 0;

   

    private async Task InsertSheetTab()

    {

 

        Count++;

 

        newTabIndex = TabInstance.Items.Count;

 

        var newSheetHeader = "Tab" + Count;

 

        List<TabItem> sheetTab = new List<TabItem>()

        {

 

            new TabItem() { Header = new TabHeader() { Text = newSheetHeader }, Content = "", TabIndex = 0 }

 

        };

 

        await TabInstance.AddTab(sheetTab, newTabIndex);

 

    }

}


For your convenience we have prepared a sample, kindly check on the sample: https://blazorplayground.syncfusion.com/VXBfDHscyaQKkpSF


Regards

Ram.




ST Steven June 18, 2024 08:18 AM UTC

It works perfectly. I didn't notice that AddTab is async.

Thank you.



RR Ram Raju Elaiyaperumal Syncfusion Team June 20, 2024 05:36 AM UTC

Hi Steven


Thanks for the update. Please get back to us if you need any further assistance.

Regards

Ram.


Loader.
Up arrow icon