|
| 1 | +import { merge } from 'rxjs/observable/merge'; |
| 2 | +import { of as observableOf } from 'rxjs/observable/of'; |
| 3 | +import { DataSource } from '@angular/cdk/collections'; |
| 4 | +import { MatPaginator, MatSort } from '@angular/material'; |
| 5 | +import { map } from 'rxjs/operators/map'; |
| 6 | +import { Observable } from 'rxjs/Observable'; |
| 7 | + |
| 8 | +// TODO: Replace this with your own data model type |
| 9 | +export interface <%= classify(name) %>Item { |
| 10 | + name: string; |
| 11 | + id: number; |
| 12 | +} |
| 13 | + |
| 14 | +// TODO: replace this with real data from your application |
| 15 | +const EXAMPLE_DATA = <%= classify(name) %>Item[] = [ |
| 16 | + {id: 1, name: 'Hydrogen'}, |
| 17 | + {id: 2, name: 'Helium'}, |
| 18 | + {id: 3, name: 'Lithium'}, |
| 19 | + {id: 4, name: 'Beryllium'}, |
| 20 | + {id: 5, name: 'Boron'}, |
| 21 | + {id: 6, name: 'Carbon'}, |
| 22 | + {id: 7, name: 'Nitrogen'}, |
| 23 | + {id: 8, name: 'Oxygen'}, |
| 24 | + {id: 9, name: 'Fluorine'}, |
| 25 | + {id: 10, name: 'Neon'}, |
| 26 | + {id: 11, name: 'Sodium'}, |
| 27 | + {id: 12, name: 'Magnesium'}, |
| 28 | + {id: 13, name: 'Aluminum'}, |
| 29 | + {id: 14, name: 'Silicon'}, |
| 30 | + {id: 15, name: 'Phosphorus'}, |
| 31 | + {id: 16, name: 'Sulfur'}, |
| 32 | + {id: 17, name: 'Chlorine'}, |
| 33 | + {id: 18, name: 'Argon'}, |
| 34 | + {id: 19, name: 'Potassium'}, |
| 35 | + {id: 20, name: 'Calcium'}, |
| 36 | +]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Data source for the <%= classify(name) %> view. This class should |
| 40 | + * encapsulate all logic for fetching and manipulating the displayed data |
| 41 | + * (including sorting, pagination, and filtering). |
| 42 | + */ |
| 43 | +export class <%= classify(name) %>DataSource extends DataSource<<%= classify(name) %>Item> { |
| 44 | + data: <%= classify(name) %>Item[] = EXAMPLE_DATA; |
| 45 | + |
| 46 | + constructor(private paginator: MatPaginator, private sort: MatSort) { |
| 47 | + super(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Connect this data source to the table. The table will only update when |
| 52 | + * the returned stream emits new items. |
| 53 | + * @returns A stream of the items to be rendered. |
| 54 | + */ |
| 55 | + connect(): Observable<<%= classify(name) %>Item[]> { |
| 56 | + // Combine everything that affects the rendered data into one update |
| 57 | + // stream for the data-table to consume. |
| 58 | + const dataMutations = [ |
| 59 | + observableOf(this.data), |
| 60 | + this.paginator.page, |
| 61 | + this.sort.sortChange |
| 62 | + ]; |
| 63 | + |
| 64 | + return merge(...dataMutations).pipe(map(() => { |
| 65 | + return this.getPagedData(this.getSortedData(this.data)); |
| 66 | + })); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Called when the table is being destroyed. Use this function, to clean up |
| 71 | + * any open connections or free any held resources that were set up during connect. |
| 72 | + */ |
| 73 | + disconnect() {} |
| 74 | + |
| 75 | + /** |
| 76 | + * Paginate the data (client-side). If you're using server-side pagination, |
| 77 | + * this would be replaced by requesting the appropriate data from the server. |
| 78 | + */ |
| 79 | + private getPagedData(data: <%= classify(name) %>Item[]) { |
| 80 | + const startIndex = this.paginator.pageIndex * this.paginator.pageSize; |
| 81 | + return data.splice(startIndex, this.paginator.pageSize); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sort the data (client-side). If you're using server-side sorting, |
| 86 | + * this would be replaced by requesting the appropriate data from the server. |
| 87 | + */ |
| 88 | + private getSortedData(data: <%= classify(name) %>Item[]) { |
| 89 | + if (!this.sort.active || this.sort.direction === '') { |
| 90 | + return data; |
| 91 | + } |
| 92 | + |
| 93 | + return data.sort((a, b) => { |
| 94 | + const isAsc = this.sort.direction == 'asc'; |
| 95 | + switch (this.sort.active) { |
| 96 | + case 'name': return compare(a.name, b.name, isAsc); |
| 97 | + case 'id': return compare(+a.id, +b.id, isAsc); |
| 98 | + default: return 0; |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** Simple sort comparator for example ID/Name columns (for client-side sorting). */ |
| 105 | +function compare(a, b, isAsc) { |
| 106 | + return (a < b ? -1 : 1) * (isAsc ? 1 : -1); |
| 107 | +} |
0 commit comments