Skip to content

Instantly share code, notes, and snippets.

@ssougnez
Created June 30, 2019 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssougnez/148981b18a11a4d39d6f9931e2254434 to your computer and use it in GitHub Desktop.
Save ssougnez/148981b18a11a4d39d6f9931e2254434 to your computer and use it in GitHub Desktop.
import { Component, Input, IterableDiffer, IterableDiffers, OnInit, DoCheck, IterableChanges } from '@angular/core';
@Component({
selector: 'data-table',
templateUrl: './data-table.component.html'
})
export class DataTableComponent implements OnInit, DoCheck {
@Input()
public data: number[] = [];
public sorted: number[] = [];
private _diff: IterableDiffer<number>;
constructor(private _iterableDiffers: IterableDiffers) { }
public ngOnInit(): void {
this._diff = this._iterableDiffers.find(this.data).create();
this._sort();
}
public ngDoCheck(): void {
const changes: IterableChanges<number> = this._diff.diff(this.data);
if (changes) {
this._sort();
}
}
private _sort(): void {
this.sorted = this.data.sort((item1: number, item2: number) => item1 - item2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment