Displaying No Records Found in Angular 2
You can use the newest feature from Angular 4.0.0 and to add if else
statement:
<div *ngIf="Games?.length;else no_data_templ">
<table>
<tbody>
<tr *ngFor="let game of Games">
<td>{{game.name}}</td>
<td>{{game.type}}</td>
</tr>
</tbody>
</table>
</div>
<ng-template #no_data_templ>
No daata found...
</ng-template>
Update: for Angular 2.X you can use the following approach:
<div *ngIf="Games?.length">
<table>
<tbody>
<tr *ngFor="let game of Games">
<td>{{game.name}}</td>
<td>{{game.type}}</td>
</tr>
</tbody>
</table>
</div>
<div *ngIf="! Games?.length">
No data found...
</div>
Check for the length
of array if there are no elements uisng length
, then display it as NO DATA
,
<li *ngIf="Games?.length == 0">
<span class="search_no_results">
No data found
</span>
</li>
If you want to display a message saying no data found. Before the table tag check whether Games has items to iterate.
Something like this
public hasData(): boolean {
return (this.Games != null && this.Games.length > 0);
}
Use hasData() in the template
<div *ngIf="!hasData()">No Data Found</div>
<table *ngIf="hasData()">
<tbody>
<tr *ngFor="let game of Games">
</td>
<td>{{game.name}}</td>
<td>{{game.type}}</td>
</tr>
</tbody>
</table>
You can structure & style this in anyway you want.