I want change the background color of the selected row after click the button I tried but change the color of all rows. This is a similar code.
HTML
<tr *ngFor="let data of (datas$ | async) | filter:authService.filter | paginate: config | orderBy: key : reverse" [ngClass]="{'data-selected':isSelected}">
<td>{{data.id}}</td>
<td>{{data.text}}</td>
<td>
<a class="mr-3" (click)="delete(data.id)"><i class="fa fa-remove"></i>
Remove
</a>
</td>
</tr>
TS
delete(postId) {
this.isSelected=true;
const ans = confirm('TEXT TEXT '+dataid);
if (ans) {
this.dataService.deleteData(postId).subscribe((data) => {
if(data) {
this.showSuccessDelete();
} else {
this.showError();
}
this.isSelected=false;
this.loadDatas();
});
}
}
CSS
.data-selected{
background-color: #e9eaea;
}
Thanks so much
The HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.
When row selection is enabled, you can set the color of selected rows using --ag-selected-row-background-color . If your grid uses alternating row colours we recommend setting this to a semi-transparent colour so that the alternating row colours are visible below it.
In HTML, table background color is defined using Cascading Style Sheets (CSS). Specifically, you use the background-color property to define background color. You can apply this property against the whole table, a row, or a single cell.
You can add an attribute to your component class and call it selectedRow, which will get the data.id.
selectedRow: number;
delete(postId,numeroDeposito) {
this.selectedRow = postId;
const ans = confirm('TEXT TEXT '+dataid);
if (ans) {
this.dataService.deleteData(postId).subscribe((data) => {
if(data) {
this.showSuccessDelete();
} else {
this.showError();
}
this.isSelected=false;
this.loadDatas();
});
}
}
then in the tr tag use [ngClass]="{'data-selected': selectedRow === data.id}"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With