Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How highlight a div on click in angular

I am having div called booking-list,which i am using to display the booking timings.Here on mouse hover the background-color of the div is changing as shown in below image.

enter image description here

Here my issue is,suppose if i click on the first timings(i,e september1) that div's background as to be changed and that background color should to be constant until i click next timings.something like this below image.I got resources for list component but i am unable to apply it for div

enter image description here

Here is the stackblitz link.

like image 561
Shankar Avatar asked Sep 17 '18 05:09

Shankar


1 Answers

Ok so this is pretty easy to do with ngClass. You make a highlight class in your css. What this will do is it will check if the condition is true and apply the css.

Then you do in the ngFor:

*ngFor="let item of item; let i = index;" (click)="setRow(i)" [ngClass]="{'highlight': selectedIndex === i}"

And then in your component:

public setRow(_index: number) { this.selectedIndex = _index;

In your css you can do something like:

.highlight{ background-color: green }

EDIT

For multi-selection you can do:

[ngClass]="{'highlight': selectedIndexs.indexOf(i)}

public setRow(_index: number) { if (this.selectedIndexs.indexOf(_index) === -1) { this.selectedIndexs.push(_index); } else { let index = this.selectedIndexs.indexOf(_index); this.selectedIndexs.splice(index, 1); }

like image 121
Swoox Avatar answered Sep 28 '22 09:09

Swoox