|
1 | | -import {CollectionViewer, DataSource} from '@angular/material'; |
| 1 | +import {CollectionViewer, DataSource, MdPaginator} from '@angular/material'; |
2 | 2 | import {Observable} from 'rxjs/Observable'; |
3 | 3 | import {PeopleDatabase, UserData} from './people-database'; |
| 4 | +import {BehaviorSubject} from 'rxjs/BehaviorSubject'; |
4 | 5 |
|
5 | 6 | export class PersonDataSource extends DataSource<any> { |
| 7 | + /** Data that should be displayed by the table. */ |
| 8 | + _displayData = new BehaviorSubject<UserData[]>([]); |
| 9 | + |
| 10 | + /** Cached data provided by the display data. */ |
6 | 11 | _renderedData: any[] = []; |
7 | 12 |
|
8 | | - constructor(private _peopleDatabase: PeopleDatabase) { |
| 13 | + constructor(private _peopleDatabase: PeopleDatabase, |
| 14 | + private _paginator: MdPaginator) { |
9 | 15 | super(); |
| 16 | + |
| 17 | + // Subscribe to page changes and database changes by clearing the cached data and |
| 18 | + // determining the updated display data. |
| 19 | + Observable.merge(this._paginator.page, this._peopleDatabase.dataChange).subscribe(() => { |
| 20 | + this._renderedData = []; |
| 21 | + this.updateDisplayData(); |
| 22 | + }); |
10 | 23 | } |
11 | 24 |
|
12 | 25 | connect(collectionViewer: CollectionViewer): Observable<UserData[]> { |
13 | | - const changeStreams = Observable.combineLatest( |
14 | | - collectionViewer.viewChange, |
15 | | - this._peopleDatabase.dataChange); |
16 | | - return changeStreams.map((result: any[]) => { |
17 | | - const view: {start: number, end: number} = result[0]; |
| 26 | + this.updateDisplayData(); |
18 | 27 |
|
19 | | - // Set the rendered rows length to the virtual page size. Fill in the data provided |
20 | | - // from the index start until the end index or pagination size, whichever is smaller. |
21 | | - this._renderedData.length = this._peopleDatabase.data.length; |
| 28 | + const streams = [collectionViewer.viewChange, this._displayData]; |
| 29 | + return Observable.combineLatest(streams) |
| 30 | + .map((results: [{start: number, end: number}, UserData[]]) => { |
| 31 | + const [view, data] = results; |
22 | 32 |
|
23 | | - const buffer = 20; |
24 | | - let rangeStart = Math.max(0, view.start - buffer); |
25 | | - let rangeEnd = Math.min(this._peopleDatabase.data.length, view.end + buffer); |
| 33 | + // Set the rendered rows length to the virtual page size. Fill in the data provided |
| 34 | + // from the index start until the end index or pagination size, whichever is smaller. |
| 35 | + this._renderedData.length = data.length; |
26 | 36 |
|
27 | | - for (let i = rangeStart; i < rangeEnd; i++) { |
28 | | - this._renderedData[i] = this._peopleDatabase.data[i]; |
29 | | - } |
| 37 | + const buffer = 20; |
| 38 | + let rangeStart = Math.max(0, view.start - buffer); |
| 39 | + let rangeEnd = Math.min(data.length, view.end + buffer); |
30 | 40 |
|
31 | | - return this._renderedData; |
32 | | - }); |
| 41 | + for (let i = rangeStart; i < rangeEnd; i++) { |
| 42 | + this._renderedData[i] = data[i]; |
| 43 | + } |
| 44 | + |
| 45 | + return this._renderedData; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + updateDisplayData() { |
| 50 | + const data = this._peopleDatabase.data.slice(); |
| 51 | + |
| 52 | + // Grab the page's slice of data. |
| 53 | + const startIndex = this._paginator.pageIndex * this._paginator.pageSize; |
| 54 | + const paginatedData = data.splice(startIndex, this._paginator.pageSize); |
| 55 | + |
| 56 | + this._displayData.next(paginatedData); |
33 | 57 | } |
34 | 58 | } |
0 commit comments