Created
June 30, 2019 18:01
-
-
Save ssougnez/148981b18a11a4d39d6f9931e2254434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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