1

How to highlight multiple selected row in angular 4 ,

Here I can edit with checkbox and perform other action too. What I am looking is to highlight the row which is checked.

<tbody>
   <tr *ngFor='let row of rowData' [ngClass]="{ 'selected': row.selected }">
     <td class="text-center>
         <input type="checkbox" [(ngModel)]="row.selected" />
     </td>
     <td>
         <input type="text" *ngIf="row.editable" [(ngModel)]="row.name" />
         <ng-container *ngIf="!row.editable">{{row.name}}</ng-container>
         <!-- You can use span or whatever instead of ng-container-->
     </td>
     <!-- Repeat for other cells -->      
   </tr>
</tbody>
Rohan Fating
  • 2,135
  • 15
  • 24
Nawab Ahmad
  • 293
  • 1
  • 4
  • 7

4 Answers4

1

It is dead simple simply use angular class directive

<tr *ngFor='let row of rowData' [class.selected]="row.selected">

Now it will add class selected when row.selected is true to the row

Sheik Althaf
  • 1,595
  • 10
  • 16
0

HTML :

<tr *ngFor="let record of mf.data; let i = index" [attr.data-index]="i" [className]=" selectedAll==true || selectedRow.indexOf(i)>-1 ?
              'pointer selected' : 'pointer'">
    <td class=" text-left" width="20">
        <input type="checkbox" (change)="selectAll(i)" [checked]="selectedAll == true">
    </td>
              ...
</tr>

TS :

export class YourComponent implements OnInit {
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
PKDEVELOP
  • 1
  • 1
0

HTML :

            <tr *ngFor="let record of mf.data; let i = index" [attr.data-index]="i" [className]=" selectedAll==true || selectedRow.indexOf(i)>-1 ?
              'pointer selected' : 'pointer'">
              <td class=" text-left" width="20">
                <input type="checkbox" (change)="selectAll(i)" [checked]="selectedAll == true">
              </td>
              ...
            </tr>

TS :

export class YourComponent implements OnInit {
selectedRow: any;
selectedAll: boolean = false;

constructor(){
   this.selectedRow = [];
}

selectAll(index) {
if (typeof (index) == 'undefined') {
  this.selectedAll = !this.selectedAll;
  this.selectedRow = [];
} else {
  this.selectedRow.push(index);
}

}

PKDEVELOP
  • 1
  • 1
  • You can [edit](https://stackoverflow.com/posts/53703971/edit) your original answer instead adding to a new one – Suraj Rao Dec 10 '18 at 10:51
-1

To highlight a row, you need to highlight the cells (<td>). From what I know, you can't highlight a row.

Here is the logic :

<tbody>
   <tr *ngFor='let row of rowData'>
     <td class="text-center" [class.selected]="row.selected">
         <input type="checkbox" [(ngModel)]="row.selected" />
     </td>
     <td [class.selected]="row.selected">
         <input type="text" *ngIf="row.editable" [(ngModel)]="row.name" />
         <ng-container *ngIf="!row.editable">{{row.name}}</ng-container>
         <!-- You can use span or whatever instead of ng-container-->
     </td>
     <!-- Repeat for other cells -->      
   </tr>
</tbody>