|
| 1 | +import { Component, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { DataSource } from '@angular/cdk'; |
| 3 | +import { MdSort } from '@angular/material'; |
| 4 | +import { BehaviorSubject } from 'rxjs/BehaviorSubject'; |
| 5 | +import { Observable } from 'rxjs/Observable'; |
| 6 | + |
| 7 | +import 'rxjs/add/operator/startWith'; |
| 8 | +import 'rxjs/add/observable/merge'; |
| 9 | +import 'rxjs/add/operator/map'; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'app-table', |
| 13 | + templateUrl: './table.component.html', |
| 14 | + styleUrls: ['./table.component.scss'] |
| 15 | +}) |
| 16 | +export class TableComponent implements OnInit { |
| 17 | + displayedColumns = ['userId', 'userName', 'progress', 'color']; |
| 18 | + database = new database(); |
| 19 | + dataSource: dataSource | null; |
| 20 | + |
| 21 | + @ViewChild(MdSort) sort: MdSort; |
| 22 | + |
| 23 | + constructor() { } |
| 24 | + |
| 25 | + ngOnInit() { |
| 26 | + this.dataSource = new dataSource(this.database, this.sort); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Local data |
| 31 | +const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple', |
| 32 | + 'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray']; |
| 33 | +const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack', |
| 34 | + 'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper', |
| 35 | + 'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth']; |
| 36 | + |
| 37 | +export interface UserData { |
| 38 | + id: string; |
| 39 | + name: string; |
| 40 | + progress: string; |
| 41 | + color: string; |
| 42 | +} |
| 43 | + |
| 44 | +export class database { |
| 45 | + // Stream that emits whenever the data has been modified |
| 46 | + dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]); |
| 47 | + get data(): UserData[] { return this.dataChange.value; } |
| 48 | + |
| 49 | + constructor() { |
| 50 | + // Fill up the database with 100 users |
| 51 | + for (let i = 0; i < 100; i++) { this.addUser(); } |
| 52 | + } |
| 53 | + |
| 54 | + // Adds a new user to the database |
| 55 | + addUser() { |
| 56 | + const copiedData = this.data.slice(); |
| 57 | + copiedData.push(this.createNewUser()); |
| 58 | + this.dataChange.next(copiedData); |
| 59 | + } |
| 60 | + |
| 61 | + // Builds and returns a new User |
| 62 | + private createNewUser() { |
| 63 | + const name = |
| 64 | + NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' + |
| 65 | + NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.'; |
| 66 | + |
| 67 | + return { |
| 68 | + id: (this.data.length + 1).toString(), |
| 69 | + name: name, |
| 70 | + progress: Math.round(Math.random() * 100).toString(), |
| 71 | + color: COLORS[Math.round(Math.random() * (COLORS.length - 1))] |
| 72 | + }; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Data source to provide what data should be rendered in the table |
| 77 | +export class dataSource extends DataSource<any> { |
| 78 | + constructor(private database: database, private sort: MdSort) { |
| 79 | + super(); |
| 80 | + } |
| 81 | + |
| 82 | + connect(): Observable<UserData[]> { |
| 83 | + const displayDataChanges = [ |
| 84 | + this.database.dataChange, |
| 85 | + this.sort.mdSortChange, |
| 86 | + ]; |
| 87 | + |
| 88 | + return Observable.merge(...displayDataChanges).map(() => { |
| 89 | + return this.getSortedData(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + disconnect() {} |
| 94 | + |
| 95 | + // Sorting the database data |
| 96 | + getSortedData(): UserData[] { |
| 97 | + const data = this.database.data.slice(); |
| 98 | + if (!this.sort.active || this.sort.direction == '') { return data; } |
| 99 | + |
| 100 | + return data.sort((a, b) => { |
| 101 | + let propertyA: number|string = ''; |
| 102 | + let propertyB: number|string = ''; |
| 103 | + |
| 104 | + switch (this.sort.active) { |
| 105 | + case 'userId': [propertyA, propertyB] = [a.id, b.id]; break; |
| 106 | + case 'userName': [propertyA, propertyB] = [a.name, b.name]; break; |
| 107 | + case 'progress': [propertyA, propertyB] = [a.progress, b.progress]; break; |
| 108 | + case 'color': [propertyA, propertyB] = [a.color, b.color]; break; |
| 109 | + } |
| 110 | + |
| 111 | + let valueA = isNaN(+propertyA) ? propertyA : +propertyA; |
| 112 | + let valueB = isNaN(+propertyB) ? propertyB : +propertyB; |
| 113 | + |
| 114 | + return (valueA < valueB ? -1 : 1) * (this.sort.direction == 'asc' ? 1 : -1); |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments