Angular 2 - highlight updated table cell

删除回忆录丶 提交于 2021-02-18 12:17:08

问题


How can i flash cell in table that changed its value?

<tr *ngFor="#product of products" (click)="onSelect(product)">
    <td>{{product.productName}}</td>
    <td>{{product.price}}</td>
</tr>

in component i have input for products : @Input() products:Array<ProductModel>; and there is a test timer in parent component that change random product`s price every 3 seconds:

var timer = Observable.timer(2000, 3000);
    timer.subscribe(t => this._changeRandomProduct());



 private _changeRandomProduct() {
    var productCandidate:ProductModel = this.products[randomOf(0, this.products.length)];
    productCandidate.price = productCandidate.price + 1;
}

how can i handle value change in price-cell to add css class on it ?


回答1:


Plunker example

You can just pass the index (or other means to identify the changed item) and add/remove a class used with a CSS animation

@Component({
  selector: 'my-products',
  styles: [`
.updated {
  animation: blinker 1.7s cubic-bezier(.5, 0, 1, 1) infinite alternate;  
}
@keyframes blinker {  
  from { opacity: 1; color: red;}
  to { opacity: 0; color: black;}
}`],
  template: `
  <tr *ngFor="let product of products; let i = index" (click)="onSelect(product)" [ngClass]="{updated: i === updatedIndex}">
    <td>{{product.productName}}</td>
    <td> - </td>
    <td>{{product.price}}</td>
  </tr>`,
})
export class MyProducts {
  @Input() products:any[];
  @Input() updatedIndex:number;
}


来源:https://stackoverflow.com/questions/37060826/angular-2-highlight-updated-table-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!