open pages in new tabs

Hi


I have a blazor wasm site that has a sidenav and a maincontent

I would like to know if there is a way to open the links in the sidenav in a dynamic tab?

2024-01-02 15_47_20-Settings.png


7 Replies

SK Satheesh Kumar Balasubramanian Syncfusion Team January 3, 2024 08:53 AM UTC

Hi Bart,

You can open pages in new tabs by adding the page to the item collections and using the two-way binding of SelectedItem as shown in the below code snippets.


Index.razor

@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.Lists
<div id="control-section">
    <div id="wrapper">
        @*Initialize the Sidebar Component*@
        <SfSidebar class="sidebar-list" Width="250px" Target=".maincontent" @bind-IsOpen="SidebarToggle" Type="@SidebarType.Auto">
            <ChildContent>
                <div class="content-area">
                    @*Initialize the ListView Component*@
                    <SfListView DataSource="@List" TValue="listData" CssClass="e-template-list">
                        <ListViewFieldSettings TValue="listData" Id="Id" Text="Text"></ListViewFieldSettings>
                        <ListViewTemplates TValue="listData">
                            <Template>
                                <div class="list-wrapper">
                                    <span class="text e-text-content">@((context as listData).Text)</span>
                                </div>
                            </Template>
                        </ListViewTemplates>
                        <ListViewEvents TValue="listData" Clicked="OnSelect"></ListViewEvents>
                    </SfListView>
                </div>
            </ChildContent>
        </SfSidebar>
        <div>
            @*Initialize the Toolbar component*@
            <SfToolbar>
                <ToolbarEvents Clicked="@Toggle"></ToolbarEvents>
                <ToolbarItems>
                    <ToolbarItem PrefixIcon="e-icons e-menu" TooltipText="Menu"></ToolbarItem>
                    <ToolbarItem>
                        <Template>
                            <div class="e-folder">
                                <div class="e-folder-name">Language</div>
                            </div>
                        </Template>
                    </ToolbarItem>
                </ToolbarItems>
            </SfToolbar>
        </div>
        <div class="maincontent">
            <div class="content">
                <SfTab LoadOn="ContentLoad.Demand" ShowCloseButton=true @bind-SelectedItem="SelectedTab">
                    <TabItems>
                        @foreach (TabData Item in TabItems)
                        {
                            <TabItem>
                                <HeaderTemplate>
                                    <div>@(Item.Header)</div>
                                </HeaderTemplate>
                                <ContentTemplate>
                                    <div>@(Item.Content)</div>
                                </ContentTemplate>
                            </TabItem>
                        }
                    </TabItems>
                </SfTab>
            </div>
        </div>
    </div>
</div>
@code {
    public class TabData
    {
        public string Header { get; set; }
        public string Content { get; set; }
    }
    List<TabData> TabItems = new List<TabData>();
    private int SelectedTab = 0;
    // Specifies the content for Right side pane.
    public string ContentText = "Before getting into any programming language, one should have basic knowledge about HTML, CSS, and JavaScript. These are the basic building blocks of web designing. HTML describes the structure of a web page whereas CSS describes the presentation of the web page";
    // Specify the value of Sidebar component state. It indicates whether the sidebar component is in an open/close state.
    public bool SidebarToggle = true;
    // Specifies the value of ListView component Datasource property.
    public List<listData> List = new List<listData>
    {
        new listData {Id="1", Text = "JavaScript", Pic = "javascript", Description = "JavaScript (JS) is an interpreted computer programming language. It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed. However, it has recently become common in both game development and the creation of desktop applications." },
        new listData {Id="2",Text = "TypeScript", Pic = "typescript", Description = "It is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript is an open-source, object-oriented programing language. It contains all elements of JavaScript. It is a language designed for large-scale JavaScript application development, which can be executed on any browser, any Host, and any Operating System. TypeScript is a language as well as a set of tools. TypeScript is the ES6 version of JavaScript with some additional features." },
        new listData {Id="3",Text = "Angular", Pic = "angular", Description = "Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Angular is written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that you import into your applications." },
        new listData {Id="4", Text = "React", Pic = "react", Description = "React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called “components”. It can also render on the server using Node." },
        new listData {Id="5", Text = "Vue", Pic = "vue", Description = "A progressive framework for building user interfaces. It is incrementally adoptable. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with modern tooling and supporting libraries." }
    };
    public class listData
    {
        public string Id { get; set; }
        public string Text { get; set; }
        public string Pic { get; set; }
        public string Description { get; set; }
    }
    // Specifies the event handler for Clicked event in ListView component.
    public void OnSelect(Syncfusion.Blazor.Lists.ClickEventArgs<listData> args)
    {
        if (args.ItemData != null)
        {
            TabItems.Add(new TabData { Header = args.ItemData.Text, Content = args.ItemData.Description });
            SelectedTab = TabItems.Count - 1;
        }
    }
    // Specifies the event handler for Clicked event in Toolbar component.
    public void Toggle(ClickEventArgs args)
    {
        if (args.Item.TooltipText == "Menu")
        {
            SidebarToggle = !SidebarToggle;
        }
    }
}

Regards,
Satheesh

Attachment: BlazorSidebarWithTab_728d9312.zip


BM Bart Malfait - Auvifox January 3, 2024 10:27 AM UTC

Hi


this is with static predefined content.


How could i be able to pass urls to the tabs?

so example:

/info => in new tab

/profile => in new tab

/about => in new tab



RR Ram Raju Elaiyaperumal Syncfusion Team January 8, 2024 03:51 PM UTC

Hi Bart,

From your query, it appears you're looking to dynamically load content from different pages into separate tabs. This can be achieved by passing URLs as parameters to the tabs and then loading the corresponding content.


I've prepared a sample that demonstrates this scenario. Please refer to the attached sample for further guidance. If you have any additional questions or need further assistance, feel free to ask.


Regards,

Ram


Attachment: BlazorSidebarWithTab_728d9312_b1dae5b7.zip


VC Veekay Consultancy July 16, 2024 07:14 AM UTC

Hi,

Can you please provide example for below:

1, when user clicks on side nav items

2. corresponding razor pages should load in main page layout

3. main page layout should show the pages in different tabs

4. the url should also change accordingly.


ie, when user clicks on each menu, corresponding pages should load in different tabs so that user can switch over.


Thanks



VR Vijay Ravi Syncfusion Team July 17, 2024 06:44 AM UTC

Hi Veekay,

We have prepared a sample to meet your requirements. To achieve this, you need to use Blazor's built-in routing and navigation. You can use the NavigationManager to navigate to different pages when the user clicks on the side nav items. When clicking items in the SideBar component, it will navigate to the corresponding Razor file, and the URL will also update as shown in the code snippet below. Refer to the shared sample for your reference. Kindly try it out.


[App.razor]


<Router AppAssembly="@typeof(Program).Assembly">

    <Found Context="routeData">

        <RouteView RouteData="@routeData" DefaultLayout="typeof(MainLayout)" />

    </Found>

    <NotFound>

        <LayoutView Layout="typeof(MainLayout)">

            <p>Sorry, there's nothing at this address.</p>

        </LayoutView>

    </NotFound>

</Router>


[index.razor]


 

@page "/"

@using BlazorSidebarWithTab.Pages

@using Syncfusion.Blazor.Navigations

@using Syncfusion.Blazor.Lists

@inject NavigationManager NavigationManager

 

<div id="control-section">

    <div id="wrapper">

        <SfSidebar class="sidebar-list" Width="250px" Target=".maincontent" @bind-IsOpen="SidebarToggle" Type="@SidebarType.Auto">

            <ChildContent>

                <div class="content-area">

                    <SfListView DataSource="@List" TValue="listData" CssClass="e-template-list">

                        <ListViewFieldSettings TValue="listData" Id="Id" Text="Text"></ListViewFieldSettings>

                        <ListViewTemplates TValue="listData">

                            <Template>

                                <div class="list-wrapper">

                                    <span class="text e-text-content">@((context as listData).Text)</span>

                                </div>

                            </Template>

                        </ListViewTemplates>

                        <ListViewEvents TValue="listData" Clicked="OnSelect"></ListViewEvents>

                    </SfListView>

                </div>

            </ChildContent>

        </SfSidebar>

    </div>

</div>

@code {   

  public void OnSelect(Syncfusion.Blazor.Lists.ClickEventArgs<listData> args)

    {

        if (args.ItemData != null)

        {

            NavigationManager.NavigateTo($"/{args.ItemData.Content}");

        }

    }

}


[React.razor]


@page "/React"

 

<h3>React</h3>

 

@code {

 

}


please get back to us if you need any further assistance


Regards,

Vijay


Attachment: updated_BlazorSidebarWithTab_512c234d.zip


BM Bart Malfait - Auvifox replied to Ram Raju Elaiyaperumal October 18, 2024 01:28 PM UTC

Hi


How can i keep the navigation inside the tab if there are links on the loaded content.

example: the angular page from your demo has a link to google in the page. 


how can you click the link so that the target (google) loads inside the same tab.



SR Subalakshmi Ramachandran Syncfusion Team October 21, 2024 02:15 PM UTC

Hi Bart,


Based on your shared details, we have understood that you need to keep the navigation page in same Tab if any link in the loaded content.  Here we have created a sample to meet your requirement. With the help of anchor and iframe tags inside the ContentTemplate of Tab, the navigation of the link will load in the same Tab.


[index.razor]

<SfTab>

     <TabItems>

         <TabItem>

             <HeaderTemplate>Syncfusion Blazor</HeaderTemplate>

             <ContentTemplate>

                 <a id="anchorlink" @ref="anchorlink" rel='nofollow' href="#" @onclick="OpenLinkInIframe">Click Link</a>

                 <br />

                 <iframe id="myIframe" src="" width="560" height="315" frameborder="0"></iframe>

             </ContentTemplate>

         </TabItem>

         ……..

        

     </TabItems>

</SfTab>
@code{

    private ElementReference anchorlink;

 

    private async Task OpenLinkInIframe()

        {

        var url = https://blazor.net;

        await JS.InvokeVoidAsync("setIframeSrc", url, anchorlink);

        }

}


Using the interop call, you can load the url in the iframe.

[Host.cshtml]

<script>

    function setIframeSrc(url,element) {

        var iframe = document.getElementById('myIframe');

        iframe.src = url;

        element.style.display = 'none';

    }

</script>


Before link click:


After link click:


Sample
: Attached as Zip folder.

Regards,

Suba R


Attachment: Blazortab7_42488fa0.zip

Loader.
Up arrow icon