-2

I am new to angular. I want to visible button of save and cancel on checkbox change event for that particular row in table. How can I set that. I am using following code :

<tr *ngFor="let configuration of configurationList;">
          <td>{{configuration.label}}</td>
          <td class="text-center">
            <mat-checkbox color='primary' name="{{configuration.id}}" [checked]="configuration.data" [(ngModel)]="configuration.data"
              (change)="onCheckedChange($event,configuration)"></mat-checkbox>
          </td>
          <td class="text-center">
            <div class="action-wrap" *ngIf="configuration.isEdit">
              <a  href="javascript:void(0);" class="icon-grid-edit p-10" matTooltip="Update!" (click)="update(configuration)">
                <i class="fa fa-check"></i>
              </a>
              <a  href="javascript:void(0);" class="icon-grid-delete p-10" matTooltip="Cancel!" (click)="cancel(configuration)">
                <i class="fa fa-times"></i>
              </a>
            </div>                                 
          </td>
        </tr>

both tag will be visible when checkbox is change and only for those row. otherwise both are hidden. If I am selecting first row checkbox and then after select second row checkbox then button are visible for second row only.

Actually data is used for display checkbox checked or not when first time load. So it will display button on already selected checkbox.

If checkbox is change then and then button will be visible.

My event code is :

onCheckedChange(event,configuration) {       
  configuration.isEdit=true;    
}

cancel(configuration) {
  this.configurationList.forEach(item => {
    item.isEdit = false;
  }
);

Thank you.

msanford
  • 11,803
  • 11
  • 66
  • 93
Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52

4 Answers4

0

Check this example for how to use ngClass Directive

https://stackblitz.com/edit/angular-eh4acx?file=index.html

 <h4>NgClass</h4>
     <ul *ngFor="let person of people">
          <li [ngClass]="{
          'text-success':person.country === 'UK',
        'text-primary':person.country === 'USA',
        'text-danger':person.country === 'HK'
         }">{{ person.name }} ({{ person.country }})
     </li>

Component.ts

  class NgClassExampleComponent {

    people: any[] = [
   {
  "name": "Douglas  Pace",
  "age": 35,
  "country": 'UK'
},
{
  "name": "Mcleod  Mueller",
  "age": 32,
  "country": 'USA'
},
{
  "name": "Day  Meyers",
  "age": 21,
  "country": 'HK'
},
{
  "name": "Aguirre  Ellis",
  "age": 34,
  "country": 'UK'
},
{
  "name": "Cook  Tyson",
  "age": 32,
  "country": 'USA'
}
];

}

Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
0

You can use ngclass to hide the element with css (display: none;)

<a [ngClass]="{'hidden': configuration.data}" href="javascript:void(0);" class="icon-grid-edit p-10" matTooltip="Update!" (click)="update(configuration)">
<i class="fa fa-check"></i>

or you can use ngif to remove the element

<a *ngIf="configuration.data" href="javascript:void(0);" class="icon-grid-delete p-10" matTooltip="Cancel!" (click)="cancel(configuration)">
<i class="fa fa-times"></i>

(if configuration.data is a boolean)

0

use [hidden] on buttons and set your expression

              <a [hidden]="configuration.data==false"  href="javascript:void(0);" class="icon-grid-edit p-10" matTooltip="Update!" (click)="update(configuration)">
                <i class="fa fa-check"></i>
              </a>
              <a [hidden]="configuration.data==false"  href="javascript:void(0);" class="icon-grid-delete p-10" matTooltip="Cancel!" (click)="cancel(configuration)">
                <i class="fa fa-times"></i>
              </a>
Arash
  • 3,458
  • 7
  • 32
  • 50
0

Re-use the same condition for checking your checkbox. Note what I added to the

<div class="action-wrap">

Style both your buttons together unless you need separate logic for each.

You can either add / remove the element from the DOM entirely:

<tr *ngFor="let configuration of configurationList;">
    <td>{{configuration.label}}</td>
    <td class="text-center">
        <mat-checkbox color='primary'
                    name="{{configuration.id}}"
                    [checked]="configuration.data"
                    [(ngModel)]="configuration.data"
                    (change)="onCheckedChange($event,configuration)">
        </mat-checkbox>
    </td>
    <td class="text-center">
        <div *ngIf="configuration.data" class="action-wrap">
            <a href="javascript:void(0);" class="icon-grid-edit p-10" matTooltip="Update!" (click)="update(configuration)">
            <i class="fa fa-check"></i>
            </a>
            <a href="javascript:void(0);" class="icon-grid-delete p-10" matTooltip="Cancel!" (click)="cancel(configuration)">
            <i class="fa fa-times"></i>
            </a>
        </div>                               
    </td>
</tr>

Or hide it with a CSS class.

<tr *ngFor="let configuration of configurationList;">
    <td>{{configuration.label}}</td>
    <td class="text-center">
    <mat-checkbox color='primary'
                    name="{{configuration.id}}"
                    [checked]="configuration.data"
                    [(ngModel)]="configuration.data"
        (change)="onCheckedChange($event,configuration)"></mat-checkbox>
    </td>
    <td class="text-center">
    <div [class.hidden]="!configuration.data" class="action-wrap">
        <a href="javascript:void(0);" class="icon-grid-edit p-10" matTooltip="Update!" (click)="update(configuration)">
        <i class="fa fa-check"></i>
        </a>
        <a href="javascript:void(0);" class="icon-grid-delete p-10" matTooltip="Cancel!" (click)="cancel(configuration)">
        <i class="fa fa-times"></i>
        </a>
    </div>                               
    </td>
</tr>

.hidden { display: hidden; }
msanford
  • 11,803
  • 11
  • 66
  • 93
  • @Srusti So, did something work? You should comment on an answer if you've implemented it, rather than the original question. It makes it too hard to follow what's going on. – msanford Apr 25 '18 at 15:53