Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
935
Dynamic tabs with routing
posted

I am using Angular 11 and latest IG (Dec 2020 release), as I am on a brand new Ignite UI license.

I am unsure how to go about dynamically creating a tab, with a routerlink to another component (with parameter).

Example:

Tab:Scenarios (list of scenarios with header info)

User clicks "Manage" in unbound "Actions" column, should:

  1. Create a tab with scenario.name as tab.title and navigate to /details/<scenario.id>
  2. Scenario List should not reload when navigated back to, unless header info changed in /details
  3. Button to delete /details tab removes from tab bar and navigates user back to most previously active tab

Parents
  • 2680
    Offline posted
    Hello Chris,

     

    Thank you for reaching out.

     

    As I understand from your description, your requirement on a high level is to initially have, let us say one tab that hosts a component, which provides the functionality to select and include other tabs – for example by name, which will be added to the parent tabs component. Please, correct me if there is a misunderstanding.

     

    To start with, I believe you will find this section of the Ignite UI for Angular documentation about Integration with Router Outlet Container for the Tabs component very helpful.

     

    In an effort to replicate such a setup, I prepared this sample, which demonstrates routing and dynamically adding tabs. As you can observe, initially there is a single “Tab 1” present. Its corresponding component is “View1Component” and it hosts an IgxCombo with two options for tabs, that can be selected. There are also buttons, upon the clicking of which the selected tabs get added to/deleted from the parent tabs component. For the sake of simplicity, the sample is not focusing on the data representation and the tabs’ object consists of simple “name” and “id” string fields.  So, since the “View1Component” is a child of the parent component, where the IgxTabs is placed and furthermore is accessed by routerLink and not a static tag, in order to implement the communication between the components, we need a service. In this case this is the “TabsService”, which consists of addedTabs and deletedTabs Subjects and public addTabs$ and deleteTabs$ Observables. The subjects emit an array of selected tab ids from within the “addTabs”/”deleteTabs” functions, when the “Generate”/”Delete” buttons in the child component are clicked. From the parent component, we can subscribe to the service’s’ add/deletetabs$ observable and push newly created tab objects to the tabs array or slice them from it.

     

    Regarding your updated requirements about implementing “details” component, and showing information about particular objects, based on a route’s parameter, my suggestion is to check out the official Angular documentation topic about In-app navigation topic here, where similar examples are provided.

     

    I hope that the sample will get you up and running with the implementation of further more complex application-level scenarios. Please, test it on your side and in case of any other questions about it, do not hesitate to address them.

     

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer
Reply Children