Skip to content

Commit aea8df4

Browse files
rafaelss95andrewseguin
authored andcommitted
docs(table): add missing imports and improve example using http (#5956)
* fix: add missing OnInit * refactor: simplify example * fix: export interfaces and remove unused import * address comments * correct nested path
1 parent c9d67c0 commit aea8df4

File tree

2 files changed

+65
-75
lines changed

2 files changed

+65
-75
lines changed

src/material-examples/table-http/table-http-example.html

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,44 @@
88
</div>
99

1010
<md-table #table [dataSource]="dataSource" class="example-table"
11-
mdSort mdSortActive="created" mdSortDisableClear mdSortDirection="asc">
11+
mdSort mdSortActive="created_at" mdSortDisableClear mdSortDirection="asc">
1212

1313
<!--- Note that these columns can be defined in any order.
1414
The actual rendered columns are set as a property on the row definition" -->
1515

1616
<!-- Number Column -->
1717
<ng-container cdkColumnDef="number">
18-
<md-header-cell *cdkHeaderCellDef> Number </md-header-cell>
19-
<md-cell *cdkCellDef="let row"> {{row.number}} </md-cell>
18+
<md-header-cell *cdkHeaderCellDef>#</md-header-cell>
19+
<md-cell *cdkCellDef="let row">{{ row.number }}</md-cell>
2020
</ng-container>
2121

2222
<!-- Title Column -->
2323
<ng-container cdkColumnDef="title">
24-
<md-header-cell *cdkHeaderCellDef> Title </md-header-cell>
25-
<md-cell *cdkCellDef="let row"> {{row.title}} </md-cell>
24+
<md-header-cell *cdkHeaderCellDef>Title</md-header-cell>
25+
<md-cell *cdkCellDef="let row">{{ row.title }}</md-cell>
2626
</ng-container>
2727

2828
<!-- State Column -->
2929
<ng-container cdkColumnDef="state">
30-
<md-header-cell *cdkHeaderCellDef> State </md-header-cell>
31-
<md-cell *cdkCellDef="let row"> {{row.state}} </md-cell>
30+
<md-header-cell *cdkHeaderCellDef>State</md-header-cell>
31+
<md-cell *cdkCellDef="let row">{{ row.state }}</md-cell>
3232
</ng-container>
3333

3434
<!-- Created Column -->
35-
<ng-container cdkColumnDef="created">
36-
<md-header-cell *cdkHeaderCellDef md-sort-header disableClear="true"> Created </md-header-cell>
37-
<md-cell *cdkCellDef="let row"> {{row.created.toDateString()}} </md-cell>
35+
<ng-container cdkColumnDef="created_at">
36+
<md-header-cell *cdkHeaderCellDef
37+
md-sort-header
38+
disableClear="true">
39+
Created
40+
</md-header-cell>
41+
<md-cell *cdkCellDef="let row">{{ row.created_at | date }}</md-cell>
3842
</ng-container>
3943

4044
<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
4145
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>
4246
</md-table>
47+
4348
<md-paginator [length]="dataSource.resultsLength"
4449
[pageSize]="30">
4550
</md-paginator>
46-
</div>
51+
</div>
Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
1-
import {Component, ViewChild} from '@angular/core';
2-
import {Http, Response} from '@angular/http';
1+
import {Component, OnInit, ViewChild} from '@angular/core';
2+
import {Http} from '@angular/http';
33
import {DataSource} from '@angular/cdk/table';
44
import {MdPaginator, MdSort} from '@angular/material';
55
import {Observable} from 'rxjs/Observable';
6-
import 'rxjs/add/operator/first';
7-
import 'rxjs/add/operator/startWith';
8-
import 'rxjs/add/operator/catch';
9-
import 'rxjs/add/operator/switchMap';
106
import 'rxjs/add/observable/merge';
117
import 'rxjs/add/observable/of';
12-
import 'rxjs/add/observable/interval';
8+
import 'rxjs/add/operator/catch';
139
import 'rxjs/add/operator/map';
10+
import 'rxjs/add/operator/startWith';
11+
import 'rxjs/add/operator/switchMap';
1412

1513
@Component({
1614
selector: 'table-http-example',
1715
styleUrls: ['table-http-example.css'],
1816
templateUrl: 'table-http-example.html',
1917
})
20-
export class TableHttpExample {
21-
displayedColumns = ['created', 'state', 'number', 'title'];
18+
export class TableHttpExample implements OnInit {
19+
displayedColumns = ['created_at', 'state', 'number', 'title'];
2220
exampleDatabase: ExampleHttpDao | null;
2321
dataSource: ExampleDataSource | null;
2422

2523
@ViewChild(MdPaginator) paginator: MdPaginator;
2624
@ViewChild(MdSort) sort: MdSort;
2725

28-
constructor(http: Http) {
29-
this.exampleDatabase = new ExampleHttpDao(http);
30-
}
26+
constructor(private http: Http) {}
3127

3228
ngOnInit() {
33-
this.dataSource = new ExampleDataSource(this.exampleDatabase!,
34-
this.sort, this.paginator);
29+
this.exampleDatabase = new ExampleHttpDao(this.http);
30+
this.dataSource = new ExampleDataSource(
31+
this.exampleDatabase!, this.paginator, this.sort);
3532
}
3633
}
3734

35+
export interface GithubApi {
36+
items: GithubIssue[];
37+
total_count: number;
38+
}
39+
3840
export interface GithubIssue {
41+
created_at: string;
3942
number: string;
4043
state: string;
4144
title: string;
42-
created: Date;
4345
}
4446

4547
/** An example database that the data source uses to retrieve data for the table. */
4648
export class ExampleHttpDao {
4749
constructor(private http: Http) {}
4850

49-
getRepoIssues(sort: string, order: string, page: number): Observable<Response> {
51+
getRepoIssues(sort: string, order: string, page: number): Observable<GithubApi> {
5052
const href = 'https://api.github.com/search/issues';
5153
const requestUrl =
52-
`${href}?q=repo:angular/material2&sort=${sort}&order=${order}&page=${page + 1}`;
53-
return this.http.get(requestUrl);
54+
`${href}?q=repo:angular/material2&sort=${sort}&order=${order}&page=${page + 1}`;
55+
56+
return this.http.get(requestUrl)
57+
.map(response => response.json() as GithubApi);
5458
}
5559
}
5660

@@ -63,67 +67,48 @@ export class ExampleHttpDao {
6367
*/
6468
export class ExampleDataSource extends DataSource<GithubIssue> {
6569
// The number of issues returned by github matching the query.
66-
resultsLength: number = 0;
70+
resultsLength = 0;
6771
isLoadingResults: boolean;
6872
isRateLimitReached: boolean;
6973

70-
constructor(private _exampleDatabase: ExampleHttpDao,
71-
private _sort: MdSort,
72-
private _paginator: MdPaginator) {
74+
constructor(private exampleDatabase: ExampleHttpDao,
75+
private paginator: MdPaginator,
76+
private sort: MdSort) {
7377
super();
7478
}
7579

7680
/** Connect function called by the table to retrieve one stream containing the data to render. */
7781
connect(): Observable<GithubIssue[]> {
7882
const displayDataChanges = [
79-
this._sort.mdSortChange,
80-
this._paginator.page,
83+
this.sort.mdSortChange,
84+
this.paginator.page
8185
];
8286

8387
// If the user changes the sort order, reset back to the first page.
84-
this._sort.mdSortChange.subscribe(() => {
85-
this._paginator.pageIndex = 0;
86-
});
88+
this.sort.mdSortChange.subscribe(() => this.paginator.pageIndex = 0);
8789

8890
return Observable.merge(...displayDataChanges)
89-
.startWith(null)
90-
.switchMap(() => {
91-
this.isLoadingResults = true;
92-
return this._exampleDatabase.getRepoIssues(
93-
this._sort.active, this._sort.direction, this._paginator.pageIndex);
94-
})
95-
.catch(() => {
96-
// Catch if the GitHub API has reached its rate limit. Return empty result.
97-
this.isRateLimitReached = true;
98-
return Observable.of(null);
99-
})
100-
.map(result => {
101-
// Flip flag to show that loading has finished.
102-
this.isLoadingResults = false;
103-
return result;
104-
})
105-
.map(result => {
106-
if (!result) { return []; }
107-
108-
this.isRateLimitReached = false;
109-
this.resultsLength = result.json().total_count;
110-
111-
return this.readGithubResult(result);
112-
});
113-
114-
91+
.startWith(null)
92+
.switchMap(() => {
93+
this.isLoadingResults = true;
94+
return this.exampleDatabase.getRepoIssues(
95+
this.sort.active, this.sort.direction, this.paginator.pageIndex);
96+
})
97+
.map(data => {
98+
// Flip flag to show that loading has finished.
99+
this.isLoadingResults = false;
100+
this.isRateLimitReached = false;
101+
this.resultsLength = data.total_count;
102+
103+
return data.items;
104+
})
105+
.catch(() => {
106+
this.isLoadingResults = false;
107+
// Catch if the GitHub API has reached its rate limit. Return empty data.
108+
this.isRateLimitReached = true;
109+
return Observable.of(null);
110+
});
115111
}
116112

117113
disconnect() {}
118-
119-
private readGithubResult(result: Response): GithubIssue[] {
120-
return result.json().items.map(issue => {
121-
return {
122-
number: issue.number,
123-
created: new Date(issue.created_at),
124-
state: issue.state,
125-
title: issue.title,
126-
};
127-
});
128-
}
129114
}

0 commit comments

Comments
 (0)