9
9
IterableChangeRecord ,
10
10
IterableDiffer ,
11
11
IterableDiffers ,
12
+ NgIterable ,
12
13
QueryList ,
13
14
ViewChild ,
14
15
ViewContainerRef ,
@@ -41,7 +42,7 @@ export class HeaderRowPlaceholder {
41
42
}
42
43
43
44
/**
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
45
46
* a header row and data rows. Updates the rows when new data is provided by the data source.
46
47
*/
47
48
@Component ( {
@@ -57,12 +58,12 @@ export class HeaderRowPlaceholder {
57
58
encapsulation : ViewEncapsulation . None ,
58
59
changeDetection : ChangeDetectionStrategy . OnPush ,
59
60
} )
60
- export class CdkTable implements CollectionViewer {
61
+ export class CdkTable < T > implements CollectionViewer {
61
62
/**
62
63
* Provides a stream containing the latest data array to render. Influenced by the table's
63
64
* stream of view window (what rows are currently on screen).
64
65
*/
65
- @Input ( ) dataSource : DataSource < any > ;
66
+ @Input ( ) dataSource : DataSource < T > ;
66
67
67
68
// TODO(andrewseguin): Remove max value as the end index
68
69
// and instead calculate the view on init and scroll.
@@ -80,7 +81,7 @@ export class CdkTable implements CollectionViewer {
80
81
private _columnDefinitionsByName = new Map < string , CdkColumnDef > ( ) ;
81
82
82
83
/** 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 ;
84
85
85
86
// Placeholders within the table's template where the header and data rows will be inserted.
86
87
@ViewChild ( RowPlaceholder ) _rowPlaceholder : RowPlaceholder ;
@@ -104,6 +105,7 @@ export class CdkTable implements CollectionViewer {
104
105
'and should be considered unstable.' ) ;
105
106
106
107
// TODO(andrewseguin): Add trackby function input.
108
+ // Find and construct an iterable differ that can be used to find the diff in an array.
107
109
this . _dataDiffer = this . _differs . find ( [ ] ) . create ( ) ;
108
110
}
109
111
@@ -132,7 +134,7 @@ export class CdkTable implements CollectionViewer {
132
134
// TODO(andrewseguin): If the data source is not
133
135
// present after view init, connect it when it is defined.
134
136
// TODO(andrewseguin): Unsubscribe from this on destroy.
135
- this . dataSource . connect ( this ) . subscribe ( ( rowsData : any [ ] ) => {
137
+ this . dataSource . connect ( this ) . subscribe ( ( rowsData : NgIterable < T > ) => {
136
138
this . renderRowChanges ( rowsData ) ;
137
139
} ) ;
138
140
}
@@ -153,7 +155,7 @@ export class CdkTable implements CollectionViewer {
153
155
}
154
156
155
157
/** Check for changes made in the data and render each change (row added/removed/moved). */
156
- renderRowChanges ( dataRows : any [ ] ) {
158
+ renderRowChanges ( dataRows : NgIterable < T > ) {
157
159
const changes = this . _dataDiffer . diff ( dataRows ) ;
158
160
if ( ! changes ) { return ; }
159
161
@@ -176,7 +178,7 @@ export class CdkTable implements CollectionViewer {
176
178
* Create the embedded view for the data row template and place it in the correct index location
177
179
* within the data row view container.
178
180
*/
179
- insertRow ( rowData : any , index : number ) {
181
+ insertRow ( rowData : T , index : number ) {
180
182
// TODO(andrewseguin): Add when predicates to the row definitions
181
183
// to find the right template to used based on
182
184
// the data rather than choosing the first row definition.
0 commit comments