In this Angular2 tutorial, we will see how to highlight selected row 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 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?

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,

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 > / h1 >



table class = "table table-hover" >

thead >

tr >

th > / th >

th > / th >

th > / th >

/ tr >

/ thead >

tbody >

= "let game of games; let i = index" ( click ) = "setClickedRow(i)" [ class .active ] = "i == selectedRow" > tr *ngForclick.active

td > / td >

td > / td >

td > / td >

/ tr >

/ tbody >

/ table >



div class = "well well-lg" >

Selected Row : strong > / strong >

/ div > {{headMessage}}Game NamePlatformRelease Date{{game.game}}{{game.platform}}{{game.release}}Selected Row :{{selectedRow}}

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 in 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.



= "let game of games; let i = index" ( click ) = "setClickedRow(i)" [ class .active ] = "i == selectedRow" > tr *ngForclick.active

td > / td >

td > / td >

td > / td >

/ tr >

..... ....{{game.game}}{{game.platform}}{{game.release}}.....

In 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 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 detail information about ngClass you can visit it’s 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 row in Angular 2 application.

More Angular 2 Stuff