How to Dynamically Add Custom Component to a tab ejs2 16.1.37

Hi,
I have created a custom component which is basically a data capturing form with input text box. grid and button.  How can I dynamically add such component to Tab 

I have tried the below  option, but the custom component is not displayed in the tab
Would appreciate any help. 
Thanks
let formcontent = '<div id="myForm"> <app-dataform></app-dataform> </div>';
this.tabObj.items.push({ header: { text: 'MyForm' }, content: formcontent });
this.tabObj.refresh();

10 Replies

AP Arun Palaniyandi Syncfusion Team May 15, 2018 05:28 AM UTC

Hi Ace,  
  
Thanks for Contacting Syncfusion support.  
  
Based on your requirement of dynamically adding the other components, we suggest your add them using the Angular <router-outlet> and then load them. 
  
  
<ejs-tab id="element" #element (selected)="select($event)" (created)=created($event) > 
  <e-tabitems> 
    <e-tabitem [header]='headerText[0]' content="#idv"></e-tabitem> 
    <e-tabitem [header]='headerText[1]' content="#idv" ></e-tabitem>  
  </e-tabitems> 
</ejs-tab> 
  
<div id="idv" > 
<router-outlet></router-outlet> 
</div> 
  
  
Ts: 
  
  public created(e){ 
  
this.tabObj.items.push({ header: { text:  'MyForm'  }, content: "#idv" });  // refer the router id 
  
(<any>this.tabObj).refresh(); 
  
} 
  
  
public select(e: any) { //based on the selection index the page will load with the dynamic components in it 
    if (e.selectedIndex == 0) {// selected index  
  
      this.router.navigateByUrl('/home'); 
  
    } 
    else if (e.selectedIndex == 1) { 
  
      this.router.navigateByUrl('/about'); 
  
    } 
      else if (e.selectedIndex == 2) { 
  
      this.router.navigateByUrl('/home'); 
  
    } 
  } 
  
  
  
  
  
  
Please check our shared sample and if you still have any query means, please let us know so that we would help you out better. 
  
  
Let us know if you have any further queries.   
  
Regards,  
Arun P.  



AC Ace May 16, 2018 04:27 PM UTC

Thanks Arun, It works as required. Appreciate your help. 


AP Arun Palaniyandi Syncfusion Team May 17, 2018 04:10 AM UTC

Hi Ace,    
   
Thanks for your update.  
  
We are glad that our shared solution has helped you out.  
  
Please let us know if you have any queries in future.  
  
Regards,  
Arun P.  
 



TU Turboshurik April 22, 2020 03:56 PM UTC

I added [showCloseButton]="true"  And after closing selected tab, it stops working: https://stackblitz.com/edit/github-lushmu-3eawqq

The problem can be avoided if you close inactive tabs. But closing the last tab also causes a problem if you add new tabs again.





VM Vengatesh Maniraj Syncfusion Team April 23, 2020 12:53 PM UTC

Hi Turboshurik, 

Greetings from Syncfusion Support. 

We have validated your reported problem “dynamic content is not updated when closing the active tab” and we are able to reproduce the issue and logged the defect report in the below feedback link. We will fix the issue and included the solution in our patch release which scheduled on the 2nd week of May 2020. 


Regards, 
Vengatesh  



VM Vengatesh Maniraj Syncfusion Team June 3, 2020 05:51 AM UTC

Hi Turboshurik,

 

Thanks for your patience.

 

We further validated your reported issue and found that the reported issue is related to sample end. Using the code snippet below we can add the dynamic custom component in tab without using router. We suggest that you follow the sample below and refer to the shared UG.

 

App.component.html

 

<hello style="display: none" id='custom'> </hello> 

 

App.component.ts

 

addDynamicTabComponent() { 

  const count: number = this.tabObj.items.length; 

  const item1: Object = { header: { text: 'Tab ' + count }, content: '#custom' }; 

  this.tabObj.addTab([item1], this.tabObj.items.length); 

  this.tabObj.dataBind(); 

  this.tabObj.select(count); 

} 

 

Sample: https://stackblitz.com/edit/angular-dynamic-tab-creation-with-component-content-molqa3

UG links:

https://ej2.syncfusion.com/angular/documentation/tab/how-to/load-tab-items-dynamically/

 https://ej2.syncfusion.com/angular/documentation/tab/how-to/adding-dynamic-items-with-content-reuse/

 

Kindly try the above links and let us know if you need further assistance.

 

Regards,

Vengatesh



TU Turboshurik July 30, 2020 02:08 PM UTC

HI Vengatesh Maniraj,

The problem is not resolved!

Steps to repeat:

  1.  Open your sample:  https://stackblitz.com/edit/angular-dynamic-tab-creation-with-component-content-molqa3
  2.  Double click on "Add tab with component content". (Added two tabs "Tab 1" and "Tab 2" with content "Hello !" ).

                

       3. Close the selected "Tab 2".


 Tab 1 is displayed incorrectly! "Hello !" content is missing. It displays "#custom". This is the id of the component tag.




RV Ravikumar Venkatesan Syncfusion Team August 3, 2020 10:50 AM UTC

Hi Turboshurik, 

Thanks for the update. 

We have validated your reported query “Hello! content is missing. It displays #custom” at our end. We have resolved your reported problem with the help of the below code. We have prepared a sample for your reference and it can be available below. 

[app.component.html] 
<ng-template #custom> 
  <hello> </hello> 
</ng-template> 

[app.component.ts] 
import { ComponentViewChildInjectViewEncapsulationTemplateRef } from '@angular/core'; 
import { TabComponent } from '@syncfusion/ej2-angular-navigations'; 
 
@Component({ 
  selector: 'app-root', 
  templateUrl: 'app.component.html', 
  styleUrls: ['app.component.css'], 
  encapsulation: ViewEncapsulation.None 
}) 
export class AppComponent { 
  @ViewChild('Tab', { static: false }) public tabObjTabComponent; 
  @ViewChild('custom', { static: false }) public customComponentTemplateRef<any>; 
 
  public dynamiccontent = '<h3>Dynamic Tab content.</h3>' 
  // Mapping Tab items Header property 
  public headerTextObject = [{ text: "Tab Header" }]; 
  public remoteFieldsObject = { text: 'Game'value: 'Id' }; 
  public datasrc = [ 
    { Id: "Game1"Game: "American Football" }, 
    { Id: "Game2"Game: "Badminton" }, 
    { Id: "Game3"Game: "Basketball" }, 
    { Id: "Game4"Game: "Cricket" }, 
    { Id: "Game5"Game: "Football" } 
  ]; 
 
  addDynamicTabComponent() { 
    const countnumber = this.tabObj.items.length; 
    const item1Object = { header: { text: 'Tab ' + count }, content: this.customComponent }; 
    this.tabObj.addTab([item1], this.tabObj.items.length); 
    this.tabObj.dataBind(); 
    this.tabObj.select(count); 
  } 
 
} 


Kindly try the above sample and get back to us if you need any further assistance. 

Regards, 
Ravikumar Venkatesan 



UN Unknown November 18, 2021 07:45 PM UTC

Hi 

I've came here with same issue, can you please give an reference or sample for passing data to the binded template. 






VD Vinitha Devi Murugan Syncfusion Team November 19, 2021 09:19 AM UTC

Hi Sai, 
 
Thanks for your update. 
 
We have checked your query “reference or sample for passing data to the binded template” and achieved your requirement using below code. We have prepared the below sample for your reference. 
 
 
App.component.html 
 
 
<ng-template #custom> 
  <hello [name]="customName"> </hello> 
</ng-template> 
 
App.component.ts 
 
 @ViewChild('custom', { static: false }) 
  public customComponentTemplateRef<any>; 
  public customName = 'test content'; 
 
addDynamicTabComponent() { 
    const countnumber = this.tabObj.items.length; 
    this.customName = 'Tab Content: ' + count// Here you can pass the data 
    const item1Object = { 
      header: { text: 'Tab ' + count }, 
      content: this.customComponent, 
    }; 
    this.tabObj.addTab([item1], this.tabObj.items.length); 
    this.tabObj.dataBind(); 
    this.tabObj.select(count); 
  } 
 
Hello.component.ts 
 
import { ComponentInput } from '@angular/core'; 
 
@Component({ 
  selector: 'hello', 
  template: `<h1>Hello {{name}}!</h1>`, 
  styles: [`h1 { font-family: Lato; }`] 
}) 
export class HelloComponent { 
  @Input() namestring; 
} 
 
Kindly try with the above sample and get back to us if you need any further assistance. 
 
Regards, 
Vinitha 


Loader.
Up arrow icon