|
1 |
| -import {Component} from '@angular/core'; |
2 |
| - |
| 1 | +import {Component, ElementRef, ViewChild} from '@angular/core'; |
| 2 | +import {DataSource} from '@angular/cdk'; |
| 3 | +import {MdPaginator, MdSort, SelectionModel} from '@angular/material'; |
| 4 | +import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
| 5 | +import {Observable} from 'rxjs/Observable'; |
| 6 | +import 'rxjs/add/operator/startWith'; |
| 7 | +import 'rxjs/add/observable/merge'; |
| 8 | +import 'rxjs/add/observable/fromEvent'; |
| 9 | +import 'rxjs/add/operator/map'; |
| 10 | +import 'rxjs/add/operator/distinctUntilChanged'; |
| 11 | +import 'rxjs/add/operator/debounceTime'; |
3 | 12 |
|
4 | 13 | @Component({
|
5 | 14 | selector: 'table-overview-example',
|
6 |
| - templateUrl: 'table-overview-example.html', |
7 | 15 | styleUrls: ['table-overview-example.css'],
|
| 16 | + templateUrl: 'table-overview-example.html', |
8 | 17 | })
|
9 | 18 | export class TableOverviewExample {
|
| 19 | + displayedColumns = ['select', 'userId', 'userName', 'progress', 'color']; |
| 20 | + exampleDatabase = new ExampleDatabase(); |
| 21 | + selection = new SelectionModel<string>(true, []); |
| 22 | + dataSource: ExampleDataSource | null; |
| 23 | + |
| 24 | + @ViewChild(MdPaginator) paginator: MdPaginator; |
| 25 | + @ViewChild(MdSort) sort: MdSort; |
| 26 | + @ViewChild('filter') filter: ElementRef; |
| 27 | + |
| 28 | + ngOnInit() { |
| 29 | + this.dataSource = new ExampleDataSource(this.exampleDatabase, this.paginator, this.sort); |
| 30 | + Observable.fromEvent(this.filter.nativeElement, 'keyup') |
| 31 | + .debounceTime(150) |
| 32 | + .distinctUntilChanged() |
| 33 | + .subscribe(() => { |
| 34 | + if (!this.dataSource) { return; } |
| 35 | + this.dataSource.filter = this.filter.nativeElement.value; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + isAllSelected(): boolean { |
| 40 | + if (!this.dataSource) { return false; } |
| 41 | + if (this.selection.isEmpty()) { return false; } |
| 42 | + |
| 43 | + if (this.filter.nativeElement.value) { |
| 44 | + return this.selection.selected.length == this.dataSource.renderedData.length; |
| 45 | + } else { |
| 46 | + return this.selection.selected.length == this.exampleDatabase.data.length; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + masterToggle() { |
| 51 | + if (!this.dataSource) { return; } |
| 52 | + |
| 53 | + if (this.isAllSelected()) { |
| 54 | + this.selection.clear(); |
| 55 | + } else if (this.filter.nativeElement.value) { |
| 56 | + this.dataSource.renderedData.forEach(data => this.selection.select(data.id)); |
| 57 | + } else { |
| 58 | + this.exampleDatabase.data.forEach(data => this.selection.select(data.id)); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** Constants used to fill up our data base. */ |
| 64 | +const COLORS = ['maroon', 'red', 'orange', 'yellow', 'olive', 'green', 'purple', |
| 65 | + 'fuchsia', 'lime', 'teal', 'aqua', 'blue', 'navy', 'black', 'gray']; |
| 66 | +const NAMES = ['Maia', 'Asher', 'Olivia', 'Atticus', 'Amelia', 'Jack', |
| 67 | + 'Charlotte', 'Theodore', 'Isla', 'Oliver', 'Isabella', 'Jasper', |
| 68 | + 'Cora', 'Levi', 'Violet', 'Arthur', 'Mia', 'Thomas', 'Elizabeth']; |
| 69 | + |
| 70 | +export interface UserData { |
| 71 | + id: string; |
| 72 | + name: string; |
| 73 | + progress: string; |
| 74 | + color: string; |
| 75 | +} |
| 76 | + |
| 77 | +/** An example database that the data source uses to retrieve data for the table. */ |
| 78 | +export class ExampleDatabase { |
| 79 | + /** Stream that emits whenever the data has been modified. */ |
| 80 | + dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]); |
| 81 | + get data(): UserData[] { return this.dataChange.value; } |
| 82 | + |
| 83 | + constructor() { |
| 84 | + // Fill up the database with 100 users. |
| 85 | + for (let i = 0; i < 100; i++) { this.addUser(); } |
| 86 | + } |
| 87 | + |
| 88 | + /** Adds a new user to the database. */ |
| 89 | + addUser() { |
| 90 | + const copiedData = this.data.slice(); |
| 91 | + copiedData.push(this.createNewUser()); |
| 92 | + this.dataChange.next(copiedData); |
| 93 | + } |
| 94 | + |
| 95 | + /** Builds and returns a new User. */ |
| 96 | + private createNewUser() { |
| 97 | + const name = |
| 98 | + NAMES[Math.round(Math.random() * (NAMES.length - 1))] + ' ' + |
| 99 | + NAMES[Math.round(Math.random() * (NAMES.length - 1))].charAt(0) + '.'; |
| 100 | + |
| 101 | + return { |
| 102 | + id: (this.data.length + 1).toString(), |
| 103 | + name: name, |
| 104 | + progress: Math.round(Math.random() * 100).toString(), |
| 105 | + color: COLORS[Math.round(Math.random() * (COLORS.length - 1))] |
| 106 | + }; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Data source to provide what data should be rendered in the table. Note that the data source |
| 112 | + * can retrieve its data in any way. In this case, the data source is provided a reference |
| 113 | + * to a common data base, ExampleDatabase. It is not the data source's responsibility to manage |
| 114 | + * the underlying data. Instead, it only needs to take the data and send the table exactly what |
| 115 | + * should be rendered. |
| 116 | + */ |
| 117 | +export class ExampleDataSource extends DataSource<any> { |
| 118 | + _filterChange = new BehaviorSubject(''); |
| 119 | + get filter(): string { return this._filterChange.value; } |
| 120 | + set filter(filter: string) { this._filterChange.next(filter); } |
| 121 | + |
| 122 | + renderedData: UserData[] = []; |
| 123 | + |
| 124 | + constructor(private _exampleDatabase: ExampleDatabase, |
| 125 | + private _paginator: MdPaginator, |
| 126 | + private _sort: MdSort) { |
| 127 | + super(); |
| 128 | + } |
| 129 | + |
| 130 | + /** Connect function called by the table to retrieve one stream containing the data to render. */ |
| 131 | + connect(): Observable<UserData[]> { |
| 132 | + // Listen for any changes in the base data, sorting, filtering, or pagination |
| 133 | + const displayDataChanges = [ |
| 134 | + this._exampleDatabase.dataChange, |
| 135 | + this._sort.mdSortChange, |
| 136 | + this._filterChange, |
| 137 | + this._paginator.page, |
| 138 | + ]; |
| 139 | + |
| 140 | + return Observable.merge(...displayDataChanges).map(() => { |
| 141 | + // Filter data |
| 142 | + const filteredData = this._exampleDatabase.data.slice().filter((item: UserData) => { |
| 143 | + let searchStr = (item.name + item.color).toLowerCase(); |
| 144 | + return searchStr.indexOf(this.filter.toLowerCase()) != -1; |
| 145 | + }); |
| 146 | + |
| 147 | + // Sort filtered data |
| 148 | + const sortedData = this.sortData(filteredData); |
| 149 | + |
| 150 | + // Grab the page's slice of the filtered sorted data. |
| 151 | + const startIndex = this._paginator.pageIndex * this._paginator.pageSize; |
| 152 | + this.renderedData = sortedData.splice(startIndex, this._paginator.pageSize); |
| 153 | + return this.renderedData; |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + disconnect() {} |
| 158 | + |
| 159 | + /** Returns a sorted copy of the database data. */ |
| 160 | + sortData(data: UserData[]): UserData[] { |
| 161 | + if (!this._sort.active || this._sort.direction == '') { return data; } |
| 162 | + |
| 163 | + return data.sort((a, b) => { |
| 164 | + let propertyA: number|string = ''; |
| 165 | + let propertyB: number|string = ''; |
| 166 | + |
| 167 | + switch (this._sort.active) { |
| 168 | + case 'userId': [propertyA, propertyB] = [a.id, b.id]; break; |
| 169 | + case 'userName': [propertyA, propertyB] = [a.name, b.name]; break; |
| 170 | + case 'progress': [propertyA, propertyB] = [a.progress, b.progress]; break; |
| 171 | + case 'color': [propertyA, propertyB] = [a.color, b.color]; break; |
| 172 | + } |
| 173 | + |
| 174 | + let valueA = isNaN(+propertyA) ? propertyA : +propertyA; |
| 175 | + let valueB = isNaN(+propertyB) ? propertyB : +propertyB; |
| 176 | + |
| 177 | + return (valueA < valueB ? -1 : 1) * (this._sort.direction == 'asc' ? 1 : -1); |
| 178 | + }); |
| 179 | + } |
10 | 180 | }
|
0 commit comments