I need to set different classes depending of the Button. Example:
delete -> class='danger';
edit -> class='primary' (setted by default)
This is my HTML:
<div class="container">
<table class="table">
<tr>
<th *ngFor="let col of columns" (click)="sortTable(col)">{{col}}</th>
<th>Actions</th>
</tr>
<tr *ngFor="let user of users | paginate: {itemsPerPage: 5,
currentPage: page,
totalItems: users.length } ; let i = index">
<td *ngFor="let col of columns">{{user[col]}}</td>
<td>
<button class="btn btn-{{class}}" *ngFor="let act of actions" (click)="actionFunc(act,i)">{{act}}</button>
//THE RESULT MUST BE LIKE THIS
<!--<button class="btn btn-danger">Delete</button>-->
<!--<button class="btn btn-primary"> Edit</button>-->
</td>
</tr>
</table>
</div>
<div>
<pagination-controls (pageChange)="page = $event"></pagination-controls>
</div>
This is my component.ts:
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-dynamic-table',
templateUrl: './dynamic-table.component.html',
styleUrls: ['./dynamic-table.component.css']
})
export class DynamicTableComponent implements OnInit {
@Input()
users = [];
@Input()
columns: string[];
@Input()
actions: string[];
@Input()
class = 'primary';
direction = false;
page: any;
constructor() {
}
sortTable(param) {
/*...*/
}
actionFunc(i, index) {
if (i === 'deleteUser') {
if (confirm('Are you sure you want to delete this item?') === true) {
this.users.splice(index, 1);
}
}
if (i === 'editUser') {
/*...*/
}
}
ngOnInit(): void {
/*if (this.actions ==='deleteUser') {
this.class = 'danger';*/ SOMETHING LIKE THIS
}
}
}
I'm not sure if I have to insert the logic inside "onInit()" so If you any suggestion I would appreciate it.