Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a newly inserted row in mat-table angular 7

Tags:

I have a mat-table where I display list of rows. I have a add new button through which user can manually add a row. Working Stackblitz: https://stackblitz.com/edit/angular-axjzov-8zmcnp I want the option to highlight the newly inserted row for 2-3 sec so that the user can see the newly created row. How do I achieve this?

like image 314
Cpt Kitkat Avatar asked Feb 04 '19 10:02

Cpt Kitkat


1 Answers

You can add this to the style css

    mat-row.mat-row {
        animation: highlight 3s;
    }

    @keyframes highlight {
      0% {
        background: red
      }
      100% {
        background: none;
      }
    }

In case you only want to highlight the new rows, you need to define the new row to add a class to them.

So let's say the class name is "highlight".

In the component you add:

export class TableFilteringExample {
    //...
    newRowIndex = 0;
    //...
    addElement() {
        this.newRowIndex++;
        //...
    }
}

In the HTML template file:

    <!--...-->

    <mat-row *matRowDef="let row; columns: displayedColumns; let i = index"
        [ngClass]="{'highlight': i < newRowIndex}"></mat-row>

    <!--...-->

And in the style file:

.highlight {
    animation: highlight 3s;
}

@keyframes highlight {
    0% {
        background: red
    }
    100% {
        background: none;
    }
}
like image 120
Nguyen Phong Thien Avatar answered Sep 22 '22 09:09

Nguyen Phong Thien