Highlight selected row in ngFor – Angular 2

In this Angular2 tutorial, we will see how to highlight selected rows in a table using ngFor & ngClass directive.
First will display all the data in the table using ngFor directive. For that, we had already written a tutorial. Please refer to below link for more details.

Also, If you are just getting started with Angular 2, here are a few tutorials and courses that can help you with the basics and setup.

DOWNLOAD

What we are building?

highlight selected row demo
highlight selected row demo

In above demo, you can see that it’s highlighting with the blue color selected row based on clicked.

Getting Started

Let’s start with clone repository first. Using below command we can easily download code and start working on that,

git clone https://github.com/Inaamhusain/Highlight-selected-row-in-ngFor-Angular-2.git highlightSelectedRow

cd highlightSelectedRow

Using above command will get all the code related to highlightSelectedRow folder and the go to that folder and then to install dependencies run following command,

npm install

To start the application run following command,

npm start

After installing and running application you can see following screen,

highlight row selected app
highlight row selected app

ngFor & ngClass Directives

Let’s start with creating home component in which display list of games in table.

app/home/home.component.ts
import {Component} from '@angular/core';
@Component({
    selector: 'app-home',
    templateUrl: './app/home/home.component.html'
})
export class HomeComponent {
    headMessage : string;
    selectedRow : Number;
    setClickedRow : Function;
    games : [{
        game: string,
        platform : string,
        release : string
    }];
    constructor(){
        this.headMessage = "Please click below rows !!";
        this.games = [{
            game : "Deus Ex: Mankind Divided",
            platform: " Xbox One, PS4, PC",
            release : "August 23"
        },
        {
            game : "Amplitude",
            platform: " PS4",
            release : "January 5"
        },
        {
            game : "The Huntsman: Winter's Curse",
            platform: "PS4",
            release : "August 23"
        },
        {
            game : "Resident Evil Zero HD Remaster",
            platform: "Win, PS3, PS4, X360, XBO",
            release : "January 19"
        },
        {
            game : "Lego Marvel's Avengers",
            platform: "Win, X360, XBO, PS3, PS4, PSVita, WiiU, 3DS",
            release : "January 26"
        }];
        this.setClickedRow = function(index){
            this.selectedRow = index;
        }
    };
};

and HTML template for home component is as follow,

app/home/home.component.html
<h1>{{headMessage}}</h1>

<table class="table table-hover">
    <thead>
        <tr>
            <th>Game Name</th>
            <th>Platform</th>
            <th>Release Date</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let game of games; let i = index" (click)="setClickedRow(i)" [class.active]="i == selectedRow">
            <td>{{game.game}}</td>
            <td>{{game.platform}}</td>
            <td>{{game.release}}</td>
        </tr>
    </tbody>
</table>

Start with home.component.ts. In which we have a list of game information which is going to display in the table. ( i.e : games (array of objects) ).
and one function setClickedRow, that will be called when we clicked on a row of the table which is going to set selected row index to the selectedRow variable.
Now will see the template for adding class active to selected row. So that it will highlight table row. Below is the small CSS code which we need to add to the CSS file.

.table tr.active td {
  background-color:#123456 !important;
  color: white;
}

In home.component.html template will add a small piece of code for adding an active class with the ngFor directive.

....
<tr *ngFor="let game of games; let i = index" (click)="setClickedRow(i)" [class.active]="i == selectedRow">
    <td>{{game.game}}</td>
    <td>{{game.platform}}</td>
    <td>{{game.release}}</td>
</tr>
.....

In the above code will use ngFor directive for looping through the data and display in the table. and then click event, when we click on a row it will set row index to the variable namely selectedRow. and for the set class to active will add the following ngClass code to the element.
[class.active]="i == selectedRow"
So we have the condition here if i is equal to selectedRow then it will set an active class for that row.

For more detailed information about ngClass you can visit its official documentation.

Now, whenever you click on any row our function setClickedRow(i) will set the value of selectedRow to the index of the row clicked, thus the ngClass argument for that row will evaluate to true and the .active class will be applied for that row.

Conclusion

In this Angular2 tutorial, we have learned to use ngClass with ngFor directive to display data and highlight selected rows in the Angular 2 application.

More Angular 2 Stuff

  1. Angular 2 Official Documentation
  2. Learn Angular 2 From our Free Video Course on YouTube
  3. Learn Angular 2 by building 12 apps
  4. Angular 2 by Istvan Novak
Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts