Read for detail : Dynamic Html Structure with - *ngIf, *ngFor & ngSwitch
you can try out , Template way below is working fine without much change, below took just 20 min
App.Component.html --- template file
<table>
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Ammount</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let ele of newTmpArr" >
<td>{{ele[0]}}</td>
<td>{{ele[1]}}</td>
<td>{{ele[2]}}</td>
<td>
<div ngSwitch="{{ele[3]}}">
<div *ngSwitchCase="'button'">
<input type="button" (click)="DoWork(1)" value="click"/>
</div>
<div *ngSwitchCase="'link'">
<a href="#" (click)="DoWork(1)" >click</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
App.Component.ts--typescript
export class AppComponent implements OnInit {
ngOnInit(): void {}
title = 'app';
DoWork()
{
//here you can do work ,
//this get called when you press "click" button in table
alert(this.title);
}
}
Without template dynamic way
It took me 5 hours to reach this code but fater that Following is working fine , you need to by pass security with the help of DomSanitizer as you want to put input element from you template file.
//typings.d.ts
interface Window { my: any; }
above one extends your window object
app.Component.html
<table>
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Ammount</th>
<th>Action</th>
</tr>
</thead>
<tbody id="tid" [innerHTML]="bodytext"></tbody>
</table>
typescript file - app.Component.ts
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import {Component, NgZone, OnInit, OnDestroy} from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
title = 'app';
_myTable : string;
bodytext ;
newTmpArr = [
["567", "2017-04-17T14:22:48", "45000", "button"],
["567", "2017-04-17T14:22:48", "45000", "button"],
["567", "2017-04-17T14:22:48", "45000", "link"],
["567", "2017-04-17T14:22:48", "45000", "button"]
];
constructor(private http: Http,private _sanitizer:
DomSanitizer,private ngZone: NgZone) {
}
ngOnInit(): void {
//below code add dynamci table to your html
for( let k=0; k<this.newTmpArr.length; k++ ){
this._myTable += "<tr>";
for( let key2 in this.newTmpArr[k] ){
if(this.newTmpArr[k][key2] == 'button' ){
//below is table click button calls angular function,
//here 1 is passed as arugment you can pass
//any other value you want
//or if you dont want to pass than just do
// onclick="my.namespace.publicFunc();"
// and remove parameter from typscript function also
this._myTable += `<td><input type="button"
onclick="my.namespace.publicFunc(1);"
value="Click me" /></td>`;
break;
}
if(this.newTmpArr[k][key2] == 'link' ){
this._myTable += `<td><a href="#"
onclick="my.namespace.publicFunc(1);
return false;">Click</a>
</td>`;
break;
} else {
this._myTable += "<td>" +
this.newTmpArr[k][key2] + "</td>";
}
}
this._myTable += "</tr>";
}
this.bodytext =
this._sanitizer.bypassSecurityTrustHtml(this._myTable);
//code to add dynamic html ends here
//below script helps to expose anglar function to javascript
window.my = window.my || {};
window.my.namespace = window.my.namespace || {};
//below line bind typescript function to window object publicFunc
window.my.namespace.publicFunc =
this.OnTableButtonClick.bind(this);
//code to expose typscipt function ends here
}
//this function get called when Table click button ge clicked
public OnTableButtonClick(input:string):void{
this.ngZone.run(() => this.DoWork(input));
}
DoWork(input:string)
{
//here you can do work ,
//this get called when you press "click" button in table
alert(this.title + input);
//this will display app1 in output
}
}