Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to change background color of table row clicked in angular?

The Following is the sample snippet of the problem

<table>
  <tr>
    <th>a</th>
    <th>b</th>
    <th>d</th>
    <th>c</th>
  </tr>
  <tr style="cursor: pointer;" *ngFor="let row of list; let i = index">
    <td>{{ row.a }}</td>
    <td>{{ row.b }}</td>
    <td>{{ row.c }}</td>
    <td>{{ row.d }}</td>
  </tr>
</table>

I have initialized the value of list in ts file.

this.list = [
          {'a': 1, 'b': 2, 'c': 3, 'd': 4},
          {'a': 1, 'b': 2, 'c': 3, 'd': 4},
          {'a': 1, 'b': 2, 'c': 3, 'd': 4}
        ];

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In angular, we can use ngClass to bind a css class to an HTML element. For this task, we can save the row index in a variable when the user click on a row and use it in ngClass to change the background color.

ngClass Documentation

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading