Reputation: 73
I can change color of a cell when i click on showProfile(). But if I click next row the previous color disappears. How can I keep the color persistent for each time I click any row?
I have this in my HTML:
<ng-container *ngIf="col === 'name'">
<mat-header-cell *matHeaderCellDef> {{ col | titlecase }} </mat-header-cell>
<mat-cell *matCellDef=" let row"
[ngClass]="{'data-selected': selectedRow ===row.id}" >
<a mat-button color="primary" (click)="showProfile(row['id'], row)"
target="_blank">{{ row[col] }}</a>
</mat-cell>
</ng-container>
And my CSS :
.data-selected {
background-color:rgb(34, 110, 34);
}
And the TS:
selectedRow = -1;
showProfile(id, row) {
this.selectedRow = id;
this.dialog.open(ViewProfileAdminComponent, {
width: '1800px',
data: {
userId: id,
isAdmin: true,
},
});
Upvotes: 0
Views: 391
Reputation: 17570
Example Demo Because u made selectedrow only one element. You need to return it to array. Each click you should insert if not and delete if exists to array and in condition u should check if it inside it.
use
[ngClass]="{'data-selected': selectedRow.indexOf(row.id) !== -1}"
and in your component change selectedRow to array
selectedRow=[];
then in your function
put below code
showProfile(id, row) {
this.selectedRow.find(x=>x==id) ? this.selectedRow=this.selectedRow.filter(x=>x!=id) : this.selectedRow.push(id);
Upvotes: 1