Your condition of [ngClass] is wrong, as it is true for all of the rows.
if you need to highlight one row at a time, you can save the row index, instead of using selRow property.
Like This:
in TS
class Component {
selectedUserIndex: number = null;
onToggleUser(userInded: number) {
// unselect the selected row, when the click was on the selected row
if(userInded === this.selectedUserIndex) {
this.selectedUserIndex = null;
return;
}
this.selectedUserIndex = userInded;
}
}
in Template
<tr *ngFor="let user of users; let userInded = index" [ngClass]="userInded == selectedUserIndex ? 'selRow' : ''">
<td class="checkbox-field"><input type="checkbox" (change)="onToggleUser(userInded)" /></td>
<td class="user-field"> <img [src]="user.userPicture" alt="">
{{ user.firstName }} {{ user.lastName }}</td>
<td class="company-field">{{ getCompany(user.companyId) }}</td>
<td class="email-field">{{ user.email }}</td>
<td class="roles-field">{{ user.permissionRoles }}</td>
<td class="edit-field">Edit</td>
<td class="delete-field"><i class="fas fa-trash-alt"></i></td>
</tr>
In the case of multiselection rows:
in TS
class Component {
selectedUsers: { [key: number]?: boolean } = {};
onToggleUser(userInded: number) {
// unselect the selected row, when the click was on the selected row
if(this.selectedUsers[userInded]) {
this.selectedUsers[userInded] = false;
return;
}
this.selectedUsers[userInded] = true;
}
}
in Template
<tr *ngFor="let user of users; let userInded = index" [ngClass]="selectedUsers[userInded] ? 'selRow' : ''">
<td class="checkbox-field"><input type="checkbox" (change)="onToggleUser(userInded)" /></td>
<td class="user-field"> <img [src]="user.userPicture" alt="">
{{ user.firstName }} {{ user.lastName }}</td>
<td class="company-field">{{ getCompany(user.companyId) }}</td>
<td class="email-field">{{ user.email }}</td>
<td class="roles-field">{{ user.permissionRoles }}</td>
<td class="edit-field">Edit</td>
<td class="delete-field"><i class="fas fa-trash-alt"></i></td>
</tr>
selRowto indicate the state of ALL rows. You need to store the state per row. The simplest that I can think of is to create a dictionary mapping based on the index.selRowis used in your .ts?ngForstatement:*ngFor="let user of users; let i = index"to get a number for each row.