EasyUI Forum

General Category => EasyUI for Angular => Topic started by: softboy99 on January 15, 2018, 08:32:43 PM



Title: How to dynamically add tabs with layout for angular?
Post by: softboy99 on January 15, 2018, 08:32:43 PM
How to dynamically add tabs with layout for angular?


Title: Re: How to dynamically add tabs with layout for angular?
Post by: jarry on January 16, 2018, 12:35:07 AM
This is the example shows how to create tab panels dynamically.
Code:
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
<eui-tabs style="height:250px">
<eui-tab-panel *ngFor="let tab of tabs" [title]="tab.title">
<p>{{tab.content}}</p>
</eui-tab-panel>
</eui-tabs>
`
})
export class AppComponent {
tabs = [{
title: 'Tab1',
content: 'Content1'
},{
title: 'Tab2',
content: 'Content2'
},{
title: 'Tab3',
content: 'Content3'
}];
}


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on January 16, 2018, 01:33:49 AM
Hi,
Thanks. Sorry for the confusion, your code is static,what i mean to dynamically add tabs and load content compent, which load data from remote server.


Title: Re: How to dynamically add tabs with layout for angular?
Post by: jarry on January 16, 2018, 06:16:53 PM
To add a new tab panel, just new a 'tabs' entry with 'content' property. This 'content' property value can be retrieved from your remote server. Please look at this example.
Code:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'app-root',
template: `
<button (click)="addTab()">add tab</button>
<eui-tabs [selectedIndex]="selectedIndex" style="height:250px">
<eui-tab-panel *ngFor="let tab of tabs" [title]="tab.title">
<div *ngIf="tab.content" [innerHTML]="tab.content"></div>
</eui-tab-panel>
</eui-tabs>
`
})
export class AppComponent {
selectedIndex = 0;

tabs:[any] = [{
title: 'Tab1',
content: 'Content1'
},{
title: 'Tab2',
content: 'Content2'
},{
title: 'Tab3',
content: 'Content3'
}];

constructor(private http: HttpClient, private sanitizer: DomSanitizer){}

addTab() {
this.http.get('...',{responseType:'text'}).subscribe(data => {
let content = this.sanitizer.bypassSecurityTrustHtml(data);
this.tabs.push({
title: 'New Tab',
content: content
});
setTimeout(() => this.selectedIndex = this.tabs.length-1);
});
}
}


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on January 16, 2018, 09:49:32 PM
Hi,
Thanks for your help. In addition, can we dynamically load another angular component,which has it own UI design and data from remote server, to the newly added tab? Any sample code?


Title: Re: How to dynamically add tabs with layout for angular?
Post by: jarry on January 17, 2018, 01:22:17 AM
You can nest any components inside a panel component. Please look at this example:
https://www.jeasyui.com/demo-angular/main/index.php?plugin=FileButton&theme=material&dir=ltr&pitem=Select%20Files&sort=


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on January 18, 2018, 10:33:45 PM
Hi,
In your sample, there is only one component. There is no component created and load dynamically. Are you sure the sample?


Title: Re: How to dynamically add tabs with layout for angular?
Post by: jarry on January 19, 2018, 12:23:13 AM
Please open https://angular.io/guide/dynamic-component-loader to learn how to add components dynamically.


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on February 01, 2018, 06:51:01 AM
Hi,
Is there api method like addTab in ts code not the template way in your sample code? Just like Jquery EasyUI?


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on February 02, 2018, 12:02:01 AM
Hi :
Could you please kindly give us a sample of this? Thanks a lot for your great help.


Title: Re: How to dynamically add tabs with layout for angular?
Post by: jarry on February 02, 2018, 12:39:58 AM
No dom operating methods are available. You should prevent from creating dom elements directly like jQuery.


Title: Re: How to dynamically add tabs with layout for angular?
Post by: softboy99 on February 02, 2018, 01:58:43 AM
Hi,
Could you give us sample for dynamically add tab and load another angular component to the added tab? I searched the web.Most of them is static way.