Skip to content

Commit 017a404

Browse files
committed
add generic to cdktable
1 parent 8c167da commit 017a404

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/lib/core/data-table/data-table.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('CdkTable', () => {
1212

1313
let component: SimpleCdkTableApp;
1414
let dataSource: FakeDataSource;
15-
let table: CdkTable;
15+
let table: CdkTable<any>;
1616
let tableElement: HTMLElement;
1717

1818
beforeEach(async(() => {
@@ -222,7 +222,7 @@ class SimpleCdkTableApp {
222222
dataSource: FakeDataSource = new FakeDataSource();
223223
columnsToRender = ['column_a', 'column_b', 'column_c'];
224224

225-
@ViewChild(CdkTable) table: CdkTable;
225+
@ViewChild(CdkTable) table: CdkTable<TestData>;
226226
}
227227

228228
function getElements(element: Element, query: string): Element[] {

src/lib/core/data-table/data-table.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IterableChangeRecord,
1010
IterableDiffer,
1111
IterableDiffers,
12+
NgIterable,
1213
QueryList,
1314
ViewChild,
1415
ViewContainerRef,
@@ -41,7 +42,7 @@ export class HeaderRowPlaceholder {
4142
}
4243

4344
/**
44-
* A data table that connects with a data source to retrieve data and renders
45+
* A data table that connects with a data source to retrieve data of type T and renders
4546
* a header row and data rows. Updates the rows when new data is provided by the data source.
4647
*/
4748
@Component({
@@ -57,12 +58,12 @@ export class HeaderRowPlaceholder {
5758
encapsulation: ViewEncapsulation.None,
5859
changeDetection: ChangeDetectionStrategy.OnPush,
5960
})
60-
export class CdkTable implements CollectionViewer {
61+
export class CdkTable<T> implements CollectionViewer {
6162
/**
6263
* Provides a stream containing the latest data array to render. Influenced by the table's
6364
* stream of view window (what rows are currently on screen).
6465
*/
65-
@Input() dataSource: DataSource<any>;
66+
@Input() dataSource: DataSource<T>;
6667

6768
// TODO(andrewseguin): Remove max value as the end index
6869
// and instead calculate the view on init and scroll.
@@ -80,7 +81,7 @@ export class CdkTable implements CollectionViewer {
8081
private _columnDefinitionsByName = new Map<string, CdkColumnDef>();
8182

8283
/** Differ used to find the changes in the data provided by the data source. */
83-
private _dataDiffer: IterableDiffer<any> = null;
84+
private _dataDiffer: IterableDiffer<T> = null;
8485

8586
// Placeholders within the table's template where the header and data rows will be inserted.
8687
@ViewChild(RowPlaceholder) _rowPlaceholder: RowPlaceholder;
@@ -104,6 +105,7 @@ export class CdkTable implements CollectionViewer {
104105
'and should be considered unstable.');
105106

106107
// TODO(andrewseguin): Add trackby function input.
108+
// Find and construct an iterable differ that can be used to find the diff in an array.
107109
this._dataDiffer = this._differs.find([]).create();
108110
}
109111

@@ -132,7 +134,7 @@ export class CdkTable implements CollectionViewer {
132134
// TODO(andrewseguin): If the data source is not
133135
// present after view init, connect it when it is defined.
134136
// TODO(andrewseguin): Unsubscribe from this on destroy.
135-
this.dataSource.connect(this).subscribe((rowsData: any[]) => {
137+
this.dataSource.connect(this).subscribe((rowsData: NgIterable<T>) => {
136138
this.renderRowChanges(rowsData);
137139
});
138140
}
@@ -153,7 +155,7 @@ export class CdkTable implements CollectionViewer {
153155
}
154156

155157
/** Check for changes made in the data and render each change (row added/removed/moved). */
156-
renderRowChanges(dataRows: any[]) {
158+
renderRowChanges(dataRows: NgIterable<T>) {
157159
const changes = this._dataDiffer.diff(dataRows);
158160
if (!changes) { return; }
159161

@@ -176,7 +178,7 @@ export class CdkTable implements CollectionViewer {
176178
* Create the embedded view for the data row template and place it in the correct index location
177179
* within the data row view container.
178180
*/
179-
insertRow(rowData: any, index: number) {
181+
insertRow(rowData: T, index: number) {
180182
// TODO(andrewseguin): Add when predicates to the row definitions
181183
// to find the right template to used based on
182184
// the data rather than choosing the first row definition.

0 commit comments

Comments
 (0)