Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - highlight updated table cell

Tags:

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 ?

like image 682
Oleg Dmytriienko Avatar asked May 05 '16 21:05

Oleg Dmytriienko


1 Answers

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;
}
like image 66
Günter Zöchbauer Avatar answered Sep 28 '22 03:09

Günter Zöchbauer