1
1
import { HttpClient } from '@angular/common/http' ;
2
- import { Component , ViewChild , AfterViewInit } from '@angular/core' ;
2
+ import { Component , ViewChild , AfterViewInit , ChangeDetectorRef } from '@angular/core' ;
3
3
import { MatPaginator } from '@angular/material/paginator' ;
4
4
import { MatSort , SortDirection } from '@angular/material/sort' ;
5
5
import { merge , Observable , of as observableOf } from 'rxjs' ;
@@ -16,7 +16,7 @@ import {catchError, map, startWith, switchMap} from 'rxjs/operators';
16
16
export class TableHttpExample implements AfterViewInit {
17
17
displayedColumns : string [ ] = [ 'created' , 'state' , 'number' , 'title' ] ;
18
18
exampleDatabase : ExampleHttpDatabase | null ;
19
- data : GithubIssue [ ] = [ ] ;
19
+ filteredAndPagedIssues : Observable < GithubIssue [ ] > ;
20
20
21
21
resultsLength = 0 ;
22
22
isLoadingResults = true ;
@@ -25,15 +25,12 @@ export class TableHttpExample implements AfterViewInit {
25
25
@ViewChild ( MatPaginator ) paginator : MatPaginator ;
26
26
@ViewChild ( MatSort ) sort : MatSort ;
27
27
28
- constructor ( private _httpClient : HttpClient ) { }
28
+ constructor ( private _httpClient : HttpClient , private _changeDetectorRef : ChangeDetectorRef ) { }
29
29
30
30
ngAfterViewInit ( ) {
31
31
this . exampleDatabase = new ExampleHttpDatabase ( this . _httpClient ) ;
32
32
33
- // If the user changes the sort order, reset back to the first page.
34
- this . sort . sortChange . subscribe ( ( ) => this . paginator . pageIndex = 0 ) ;
35
-
36
- merge ( this . sort . sortChange , this . paginator . page )
33
+ this . filteredAndPagedIssues = merge ( this . sort . sortChange , this . paginator . page )
37
34
. pipe (
38
35
startWith ( { } ) ,
39
36
switchMap ( ( ) => {
@@ -57,7 +54,12 @@ export class TableHttpExample implements AfterViewInit {
57
54
this . resultsLength = data . total_count ;
58
55
return data . items ;
59
56
} )
60
- ) . subscribe ( data => this . data = data ) ;
57
+ ) ;
58
+ this . _changeDetectorRef . detectChanges ( ) ;
59
+ }
60
+
61
+ resetPaging ( ) : void {
62
+ this . paginator . pageIndex = 0 ;
61
63
}
62
64
}
63
65
0 commit comments