-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(table): add example using http (#5766)
Fixes #5670
- Loading branch information
1 parent
35eb294
commit 8c1e803
Showing
4 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Structure */ | ||
.example-container { | ||
display: flex; | ||
flex-direction: column; | ||
max-height: 500px; | ||
min-width: 300px; | ||
position: relative; | ||
} | ||
|
||
.example-header { | ||
min-height: 64px; | ||
display: flex; | ||
align-items: center; | ||
padding-left: 24px; | ||
font-size: 20px; | ||
} | ||
|
||
.example-table { | ||
overflow: auto; | ||
min-height: 300px; | ||
} | ||
|
||
.mat-column-title { | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
flex: 1; | ||
overflow: hidden; | ||
} | ||
|
||
/* Column Widths */ | ||
.mat-column-number, | ||
.mat-column-state { | ||
max-width: 64px; | ||
} | ||
|
||
.mat-column-created { | ||
max-width: 124px; | ||
} | ||
|
||
.example-loading-shade { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
bottom: 56px; | ||
right: 0; | ||
background: rgba(0, 0, 0, 0.15); | ||
z-index: 1; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.example-rate-limit-reached { | ||
color: #980000; | ||
max-width: 360px; | ||
text-align: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<div class="example-container mat-elevation-z8"> | ||
<div class="example-loading-shade" | ||
*ngIf="dataSource.isLoadingResults || dataSource.isRateLimitReached"> | ||
<md-spinner *ngIf="dataSource.isLoadingResults"></md-spinner> | ||
<div class="example-rate-limit-reached" *ngIf="dataSource.isRateLimitReached"> | ||
GitHub's API rate limit has been reached. It will be reset in one minute. | ||
</div> | ||
</div> | ||
|
||
<md-table #table [dataSource]="dataSource" class="example-table" | ||
mdSort mdSortActive="created" mdSortDisableClear mdSortDirection="asc"> | ||
|
||
<!--- Note that these columns can be defined in any order. | ||
The actual rendered columns are set as a property on the row definition" --> | ||
|
||
<!-- Number Column --> | ||
<ng-container cdkColumnDef="number"> | ||
<md-header-cell *cdkHeaderCellDef> Number </md-header-cell> | ||
<md-cell *cdkCellDef="let row"> {{row.number}} </md-cell> | ||
</ng-container> | ||
|
||
<!-- Title Column --> | ||
<ng-container cdkColumnDef="title"> | ||
<md-header-cell *cdkHeaderCellDef> Title </md-header-cell> | ||
<md-cell *cdkCellDef="let row"> {{row.title}} </md-cell> | ||
</ng-container> | ||
|
||
<!-- State Column --> | ||
<ng-container cdkColumnDef="state"> | ||
<md-header-cell *cdkHeaderCellDef> State </md-header-cell> | ||
<md-cell *cdkCellDef="let row"> {{row.state}} </md-cell> | ||
</ng-container> | ||
|
||
<!-- Created Column --> | ||
<ng-container cdkColumnDef="created"> | ||
<md-header-cell *cdkHeaderCellDef md-sort-header disableClear="true"> Created </md-header-cell> | ||
<md-cell *cdkCellDef="let row"> {{row.created.toDateString()}} </md-cell> | ||
</ng-container> | ||
|
||
<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row> | ||
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row> | ||
</md-table> | ||
<md-paginator [length]="dataSource.resultsLength" | ||
[pageSize]="30"> | ||
</md-paginator> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import {Component, ViewChild} from '@angular/core'; | ||
import {Http, Response} from '@angular/http'; | ||
import {DataSource} from '@angular/cdk'; | ||
import {MdPaginator, MdSort} from '@angular/material'; | ||
import {Observable} from 'rxjs/Observable'; | ||
import 'rxjs/add/operator/first'; | ||
import 'rxjs/add/operator/startWith'; | ||
import 'rxjs/add/operator/catch'; | ||
import 'rxjs/add/operator/switchMap'; | ||
import 'rxjs/add/observable/merge'; | ||
import 'rxjs/add/observable/of'; | ||
import 'rxjs/add/observable/interval'; | ||
import 'rxjs/add/operator/map'; | ||
|
||
@Component({ | ||
selector: 'table-http-example', | ||
styleUrls: ['table-http-example.css'], | ||
templateUrl: 'table-http-example.html', | ||
}) | ||
export class TableHttpExample { | ||
displayedColumns = ['created', 'state', 'number', 'title']; | ||
exampleDatabase: ExampleHttpDao | null; | ||
dataSource: ExampleDataSource | null; | ||
|
||
@ViewChild(MdPaginator) paginator: MdPaginator; | ||
@ViewChild(MdSort) sort: MdSort; | ||
|
||
constructor(http: Http) { | ||
this.exampleDatabase = new ExampleHttpDao(http); | ||
} | ||
|
||
ngOnInit() { | ||
this.dataSource = new ExampleDataSource(this.exampleDatabase!, | ||
this.sort, this.paginator); | ||
} | ||
} | ||
|
||
export interface GithubIssue { | ||
number: string; | ||
state: string; | ||
title: string; | ||
created: Date; | ||
} | ||
|
||
/** An example database that the data source uses to retrieve data for the table. */ | ||
export class ExampleHttpDao { | ||
constructor(private http: Http) {} | ||
|
||
getRepoIssues(sort: string, order: string, page: number): Observable<Response> { | ||
const href = 'https://api.github.com/search/issues'; | ||
const requestUrl = | ||
`${href}?q=repo:angular/material2&sort=${sort}&order=${order}&page=${page + 1}`; | ||
return this.http.get(requestUrl); | ||
} | ||
} | ||
|
||
/** | ||
* Data source to provide what data should be rendered in the table. Note that the data source | ||
* can retrieve its data in any way. In this case, the data source is provided a reference | ||
* to a common data base, ExampleHttpDao. It is not the data source's responsibility to manage | ||
* the underlying data. Instead, it only needs to take the data and send the table exactly what | ||
* should be rendered. | ||
*/ | ||
export class ExampleDataSource extends DataSource<GithubIssue> { | ||
// The number of issues returned by github matching the query. | ||
resultsLength: number = 0; | ||
isLoadingResults: boolean; | ||
isRateLimitReached: boolean; | ||
|
||
constructor(private _exampleDatabase: ExampleHttpDao, | ||
private _sort: MdSort, | ||
private _paginator: MdPaginator) { | ||
super(); | ||
} | ||
|
||
/** Connect function called by the table to retrieve one stream containing the data to render. */ | ||
connect(): Observable<GithubIssue[]> { | ||
const displayDataChanges = [ | ||
this._sort.mdSortChange, | ||
this._paginator.page, | ||
]; | ||
|
||
// If the user changes the sort order, reset back to the first page. | ||
this._sort.mdSortChange.subscribe(() => { | ||
this._paginator.pageIndex = 0; | ||
}); | ||
|
||
return Observable.merge(...displayDataChanges) | ||
.startWith(null) | ||
.switchMap(() => { | ||
this.isLoadingResults = true; | ||
return this._exampleDatabase.getRepoIssues( | ||
this._sort.active, this._sort.direction, this._paginator.pageIndex); | ||
}) | ||
.catch(() => { | ||
// Catch if the GitHub API has reached its rate limit. Return empty result. | ||
this.isRateLimitReached = true; | ||
return Observable.of(null); | ||
}) | ||
.map(result => { | ||
// Flip flag to show that loading has finished. | ||
this.isLoadingResults = false; | ||
return result; | ||
}) | ||
.map(result => { | ||
if (!result) { return []; } | ||
|
||
this.isRateLimitReached = false; | ||
this.resultsLength = result.json().total_count; | ||
|
||
return this.readGithubResult(result); | ||
}); | ||
|
||
|
||
} | ||
|
||
disconnect() {} | ||
|
||
private readGithubResult(result: Response): GithubIssue[] { | ||
return result.json().items.map(issue => { | ||
return { | ||
number: issue.number, | ||
created: new Date(issue.created_at), | ||
state: issue.state, | ||
title: issue.title, | ||
}; | ||
}); | ||
} | ||
} |
8c1e803
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably make a lot of sense if someone explained why this is so complicated? If i use Kendo UI Grid i just bind to a collection (with a little sort config)
8c1e803
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the "complexity" now is because it is a extremely generic implementation that allows everything to be done (not just a few scenarios or the most common ones). In the future, multiple extensions of "DataSource" could be written to handle specific use cases, minimizing the need of custom code.
Am I wrong? This is the way I see Angular Material, and that's why I really like the project: it's not "just another UI framework to do the same things all others do". The team is commited to work with best practices and are designing based on a very promising way to work with architectural patterns.
8c1e803
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in agreement. All it really boils down to is getting an
Observable<T[]>
out ofconnect()
, and everything else is implementation-specific. I suppose it could be simpler, but the team is really pushing the idea of decoupling presentation from data source, which I think users will come to appreciate as their implementations become more complex.I think reading this thread is helpful #5883, and I've opened an issue for a barebones, single-render example to be added to the docs #6036.
8c1e803
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The information in those threads is very useful. I'd had in look in https://github.com/angular/material2/wiki/Design-doc-directory but there was nothing... if you are going to publish a new component then people really need to be advised the how and why it works as it does so as they can evaluate it effectively (or be able to find the how and why!!!)
That said, consumers of this component are not likely to care very much about these reasons in the majority of cases - i.e. one time simple observable with all the data they require. I think you need to be careful this is not clever people engineering clever solutions where a simple solution will do?