如何在 mat-table angular 7 中突出显示新插入的行

Saa*_*med 5 html typescript angular-material angular

我有一个 mat-table,用于显示行列表。我有一个添加新按钮,用户可以通过该按钮手动添加一行。工作 Stackblitz:https ://stackblitz.com/edit/angular-axjzov-8zmcnp 我想要突出显示新插入的行 2-3 秒的选项,以便用户可以看到新创建的行。我如何实现这一目标?

Ngu*_*ien 4

您可以将其添加到样式 css

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

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

Run Code Online (Sandbox Code Playgroud)

如果您只想突出显示新行,则需要定义新行以向其添加类。

假设类名称是“highlight”。

在您添加的组件中:

export class TableFilteringExample {
    //...
    newRowIndex = 0;
    //...
    addElement() {
        this.newRowIndex++;
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

在 HTML 模板文件中:

    <!--...-->

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

    <!--...-->
Run Code Online (Sandbox Code Playgroud)

在样式文件中:

.highlight {
    animation: highlight 3s;
}

@keyframes highlight {
    0% {
        background: red
    }
    100% {
        background: none;
    }
}
Run Code Online (Sandbox Code Playgroud)