user741825
user741825

Reputation: 89

Switch the contents of "mat-cell" based on element's value

Is there any way to check the value of element's property (in this case element.VerificationCode) and based on that, switch the contents of cell?

I need to show the VerificationCode in cell and if the element.VerificationCode is null then show the button to generate one.

Example

<ng-container matColumnDef="VerificationCode">
  <th mat-header-cell *matHeaderCellDef mat-sort-header> Family code </th>
     <td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()
       <!-- 1 -->
         {{element.VerificationCode}}
       <!-- 2 -->        
         <button  mat-stroked-button (click)="genVerificationCode(group.id)">
             Generate 
         </button>         
 </td>
</ng-container>

Upvotes: 2

Views: 968

Answers (2)

Jeto
Jeto

Reputation: 14927

Alternative to @TeddySterne's version (I believe this is the preferred way of doing it in recent versions of Angular, but I could be wrong):

<td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
  <ng-container *ngIf="element.VerificationCode">{{element.VerificationCode}}</ng-container>
  <button *ngIf="!element.VerificationCode" mat-stroked-button (click)="genVerificationCode(group.id)">Generate</button>        
</td>

Upvotes: 1

Teddy Sterne
Teddy Sterne

Reputation: 14221

You can use an ngIf with an else statement to accomplish it. Example:

<td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
 <span *ngIf="element.VerificationCode; else showButton">{{element.VerificationCode}}</span>
 <ng-template #showButton>
   <button mat-stroked-button (click)="genVerificationCode(group.id)">
       Generate 
   </button>  
 </ng-template>       
</td>

Upvotes: 0

Related Questions