Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Change BackGround Color of Row in Table After click the Buttom

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

like image 517
Toriga Avatar asked Nov 28 '19 09:11

Toriga


People also ask

How do I change the background color of a row in a table?

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.

How do I change the selected row color in Ag grid?

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.

Can we set a background color just for one cell in a table?

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.


1 Answers

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}".

like image 194
a bouchenafa Avatar answered Sep 20 '22 22:09

a bouchenafa