Skip to content

Commit e14d0ec

Browse files
committed
feat(table): add MdTableDataSource
1 parent 1b6b270 commit e14d0ec

File tree

9 files changed

+615
-38
lines changed

9 files changed

+615
-38
lines changed

src/cdk/rxjs/rx-operators.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Observable, ObservableInput} from 'rxjs/Observable';
9+
// Subscribable needs to be imported because combineLatest has or is using it, but it does not need
10+
// to be used in any of the explicit typings in this file.
11+
// tslint:disable-next-line:no-unused-variable
12+
import {Observable, ObservableInput, Subscribable} from 'rxjs/Observable';
1013
import {PartialObserver} from 'rxjs/Observer';
1114
import {Subscription} from 'rxjs/Subscription';
1215
import {IScheduler} from 'rxjs/Scheduler';
@@ -22,6 +25,7 @@ import {startWith as startWithOperator} from 'rxjs/operator/startWith';
2225
import {debounceTime as debounceTimeOperator} from 'rxjs/operator/debounceTime';
2326
import {auditTime as auditTimeOperator} from 'rxjs/operator/auditTime';
2427
import {takeUntil as takeUntilOperator} from 'rxjs/operator/takeUntil';
28+
import {combineLatest as combineLatestOperator} from 'rxjs/operator/combineLatest';
2529

2630
/**
2731
* Represents a strongly-typed chain of RxJS operators.
@@ -71,6 +75,9 @@ export interface StrictRxChain<T> {
7175

7276
call(operator: takeUntilOperatorType<T>, notifier: Observable<any>): StrictRxChain<T>;
7377

78+
call<T2>(operator: combineLatestOperatorType<T, T2>,
79+
v2: ObservableInput<T2>): StrictRxChain<[T, T2]>;
80+
7481
subscribe(fn: (t: T) => void): Subscription;
7582

7683
result(): Observable<T>;
@@ -89,6 +96,7 @@ export class StartWithBrand { private _; }
8996
export class DebounceTimeBrand { private _; }
9097
export class AuditTimeBrand { private _; }
9198
export class TakeUntilBrand { private _; }
99+
export class CombineLatestBrand { private _; }
92100
/* tslint:enable:no-unused-variable */
93101

94102

@@ -104,8 +112,9 @@ export type startWithOperatorType<T> = typeof startWithOperator & StartWithBrand
104112
export type debounceTimeOperatorType<T> = typeof debounceTimeOperator & DebounceTimeBrand;
105113
export type auditTimeOperatorType<T> = typeof auditTimeOperator & AuditTimeBrand;
106114
export type takeUntilOperatorType<T> = typeof takeUntilOperator & TakeUntilBrand;
115+
export type combineLatestOperatorType<T, R> = typeof combineLatestOperator & CombineLatestBrand;
107116

108-
// We add `Function` to the type intersection to make this nomically different from
117+
// We add `Function` to the type intersection to make this nominally different from
109118
// `finallyOperatorType` while still being structurally the same. Without this, TypeScript tries to
110119
// reduce `typeof _finallyOperator & FinallyBrand` to `finallyOperatorType<T>` and then fails
111120
// because `T` isn't known.
@@ -123,3 +132,5 @@ export const debounceTime =
123132
debounceTimeOperator as typeof debounceTimeOperator & DebounceTimeBrand & Function;
124133
export const auditTime = auditTimeOperator as typeof auditTimeOperator & AuditTimeBrand & Function;
125134
export const takeUntil = takeUntilOperator as typeof takeUntilOperator & TakeUntilBrand & Function;
135+
export const combineLatest =
136+
combineLatestOperator as typeof combineLatestOperator & CombineLatestBrand & Function;

src/demo-app/table/table-demo.html

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,62 @@ <h3>MdTable Example</h3>
170170
[pageSizeOptions]="[5, 10, 25, 100]">
171171
</md-paginator>
172172
</div>
173+
174+
175+
<h3>MdTable With MdTableDataSource Example</h3>
176+
177+
<md-form-field>
178+
<input mdInput [formControl]="filter">
179+
</md-form-field>
180+
181+
<div class="demo-table-container demo-mat-table-example mat-elevation-z4">
182+
183+
<table-header-demo (shiftColumns)="displayedColumns.push(displayedColumns.shift())"
184+
(toggleColorColumn)="toggleColorColumn()">
185+
</table-header-demo>
186+
187+
<md-table [dataSource]="mdTableDataSource" [trackBy]="userTrackBy" mdSort
188+
#sortForDataSource="mdSort">
189+
190+
<!-- Column Definition: ID -->
191+
<ng-container cdkColumnDef="userId">
192+
<md-header-cell *mdHeaderCellDef md-sort-header> ID </md-header-cell>
193+
<md-cell *mdCellDef="let row"> {{row.id}} </md-cell>
194+
</ng-container>
195+
196+
<!-- Column Definition: Progress -->
197+
<ng-container mdColumnDef="progress">
198+
<md-header-cell *mdHeaderCellDef md-sort-header> Progress </md-header-cell>
199+
<md-cell *mdCellDef="let row">
200+
<div class="demo-progress-stat">{{row.progress}}%</div>
201+
<div class="demo-progress-indicator-container">
202+
<div class="demo-progress-indicator"
203+
[style.background]="row.progress > 50 ? 'green' : 'red'"
204+
[style.opacity]="getOpacity(row.progress)"
205+
[style.width.%]="row.progress"></div>
206+
</div>
207+
</md-cell>
208+
</ng-container>
209+
210+
<!-- Column Definition: Name -->
211+
<ng-container mdColumnDef="userName">
212+
<md-header-cell *mdHeaderCellDef md-sort-header> Name </md-header-cell>
213+
<md-cell *mdCellDef="let row"> {{row.name}} </md-cell>
214+
</ng-container>
215+
216+
<!-- Column Definition: Color -->
217+
<ng-container mdColumnDef="color">
218+
<md-header-cell *mdHeaderCellDef md-sort-header>Color</md-header-cell>
219+
<md-cell *mdCellDef="let row" [style.color]="row.color"> {{row.color}} </md-cell>
220+
</ng-container>
221+
222+
<md-header-row *mdHeaderRowDef="displayedColumns"></md-header-row>
223+
<md-row *mdRowDef="let row; columns: displayedColumns"></md-row>
224+
225+
</md-table>
226+
227+
<md-paginator #paginatorForDataSource
228+
[length]="_peopleDatabase.data.length"
229+
[pageSizeOptions]="[10, 25, 100]">
230+
</md-paginator>
231+
</div>

src/demo-app/table/table-demo.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {Component, ViewChild} from '@angular/core';
22
import {PeopleDatabase, UserData} from './people-database';
33
import {PersonDataSource} from './person-data-source';
4-
import {MdPaginator, MdSort} from '@angular/material';
4+
import {MdPaginator, MdSort, MdTableDataSource} from '@angular/material';
5+
import {FormControl} from '@angular/forms';
56

67
export type UserProperties = 'userId' | 'userName' | 'progress' | 'color' | undefined;
78

@@ -17,19 +18,44 @@ const properties = ['id', 'name', 'progress', 'color'];
1718
})
1819
export class TableDemo {
1920
dataSource: PersonDataSource | null;
21+
mdTableDataSource = new MdTableDataSource<UserData>();
2022
displayedColumns: UserProperties[] = [];
2123
trackByStrategy: TrackByStrategy = 'reference';
2224
changeReferences = false;
2325
highlights = new Set<string>();
2426

27+
filter = new FormControl();
28+
2529
dynamicColumnDefs: any[] = [];
2630
dynamicColumnIds: string[] = [];
2731

28-
@ViewChild(MdPaginator) _paginator: MdPaginator;
29-
32+
@ViewChild(MdPaginator) paginator: MdPaginator;
3033
@ViewChild(MdSort) sort: MdSort;
3134

32-
constructor(public _peopleDatabase: PeopleDatabase) { }
35+
@ViewChild('paginatorForDataSource') paginatorForDataSource: MdPaginator;
36+
@ViewChild('sortForDataSource') sortForDataSource: MdSort;
37+
38+
constructor(public _peopleDatabase: PeopleDatabase) {
39+
this.mdTableDataSource
40+
this.mdTableDataSource.sortingDataAccessor = (data: UserData, property: string) => {
41+
switch (property) {
42+
case 'userId': return +data.id;
43+
case 'userName': return data.name;
44+
case 'progress': return +data.progress;
45+
case 'color': return data.color;
46+
default: return '';
47+
}
48+
};
49+
this.mdTableDataSource.filterTermAccessor = (data: UserData) => data.name;
50+
this.filter.valueChanges.subscribe(filter => this.mdTableDataSource!.filter = filter);
51+
}
52+
53+
ngAfterViewInit() {
54+
// Needs to be set up after the view is initialized since the data source will look at the sort
55+
// and paginator's initial values to know what data should be rendered.
56+
this.mdTableDataSource!.paginator = this.paginatorForDataSource;
57+
this.mdTableDataSource!.sort = this.sortForDataSource;
58+
}
3359

3460
ngOnInit() {
3561
this.connect();
@@ -54,8 +80,9 @@ export class TableDemo {
5480
connect() {
5581
this.displayedColumns = ['userId', 'userName', 'progress', 'color'];
5682
this.dataSource = new PersonDataSource(this._peopleDatabase,
57-
this._paginator, this.sort);
83+
this.paginator, this.sort);
5884
this._peopleDatabase.initialize();
85+
this.mdTableDataSource!.data = this._peopleDatabase.data.slice();
5986
}
6087

6188
disconnect() {

src/lib/core/rxjs/rx-operators.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
DebounceTimeBrand,
2121
AuditTimeBrand,
2222
TakeUntilBrand,
23+
CombineLatestBrand,
2324
finallyOperatorType,
2425
catchOperatorType,
2526
doOperatorType,
@@ -32,6 +33,7 @@ export {
3233
debounceTimeOperatorType,
3334
auditTimeOperatorType,
3435
takeUntilOperatorType,
36+
combineLatestOperatorType,
3537
finallyOperator,
3638
catchOperator,
3739
doOperator,
@@ -44,4 +46,5 @@ export {
4446
debounceTime,
4547
auditTime,
4648
takeUntil,
49+
combineLatest,
4750
} from '@angular/cdk/rxjs';

src/lib/sort/sort.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Sort {
2525
/** Container for MdSortables to manage the sort state and provide default sort parameters. */
2626
@Directive({
2727
selector: '[mdSort], [matSort]',
28+
exportAs: 'mdSort'
2829
})
2930
export class MdSort {
3031
/** Collection of all registered sortables that this directive manages. */

src/lib/table/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {MdCommonModule} from '../core';
1717
export * from './cell';
1818
export * from './table';
1919
export * from './row';
20+
export * from './table-data-source';
2021

2122
@NgModule({
2223
imports: [CdkTableModule, CommonModule, MdCommonModule],

0 commit comments

Comments
 (0)