@Component({
templateUrl: './tablestyledemo.html',
styles: [`
.old-car {
background-color: #1CA979 !important;
color: #ffffff !important;
}
.very-old-car {
background-color: #2CA8B1 !important;
color: #ffffff !important;
}
`
]
})
export class TableStyleDemo implements OnInit {
cars: Car[];
cols: any[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsSmall().then(cars => this.cars = cars);
this.cols = [
{ field: 'vin', header: 'Vin' },
{ field: 'year', header: 'Year' },
{ field: 'brand', header: 'Brand' },
{ field: 'color', header: 'Color' }
];
}
}
<p-table [columns]="cols" [value]="cars">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
<tr [ngClass]="rowData.year > 2010 ? 'new-car' : null">
<td *ngFor="let col of columns" [ngClass]="rowData[col.field] <= 2010 ? 'old-car' : null">
{{rowData[col.field]}}
</td>
</tr>
</ng-template>
</p-table>