@@ -31,7 +31,6 @@ import {
31
31
} from '@angular/core' ;
32
32
import { CollectionViewer , DataSource } from './data-source' ;
33
33
import { CdkCellOutlet , CdkCellOutletRowContext , CdkHeaderRowDef , CdkRowDef } from './row' ;
34
- import { merge } from 'rxjs/observable/merge' ;
35
34
import { takeUntil } from 'rxjs/operator/takeUntil' ;
36
35
import { BehaviorSubject } from 'rxjs/BehaviorSubject' ;
37
36
import { Subscription } from 'rxjs/Subscription' ;
@@ -174,6 +173,27 @@ export class CdkTable<T> implements CollectionViewer {
174
173
}
175
174
}
176
175
176
+ ngOnInit ( ) {
177
+ // TODO(andrewseguin): Setup a listener for scrolling, emit the calculated view to viewChange
178
+ this . _dataDiffer = this . _differs . find ( [ ] ) . create ( this . _trackByFn ) ;
179
+ }
180
+
181
+ ngAfterContentInit ( ) {
182
+ this . _updateColumnDefinitions ( ) ;
183
+ this . _columnDefinitions . changes . subscribe ( ( ) => this . _updateColumnDefinitions ( ) ) ;
184
+ }
185
+
186
+ ngAfterContentChecked ( ) {
187
+ this . _checkColumnsChange ( ) ;
188
+ }
189
+
190
+ ngAfterViewInit ( ) {
191
+ this . _isViewInitialized = true ;
192
+ if ( this . dataSource && ! this . _renderChangeSubscription ) {
193
+ this . _observeRenderChanges ( ) ;
194
+ }
195
+ }
196
+
177
197
ngOnDestroy ( ) {
178
198
this . _onDestroy . next ( ) ;
179
199
this . _onDestroy . complete ( ) ;
@@ -182,52 +202,34 @@ export class CdkTable<T> implements CollectionViewer {
182
202
}
183
203
}
184
204
185
- ngOnInit ( ) {
186
- // TODO(andrewseguin): Setup a listener for scroll events
187
- // and emit the calculated view to this.viewChange
188
- }
189
-
190
- ngAfterContentInit ( ) {
205
+ /** Update the map containing the content's column definitions. */
206
+ private _updateColumnDefinitions ( ) {
191
207
// TODO(andrewseguin): Throw an error if two columns share the same name
192
208
this . _columnDefinitions . forEach ( columnDef => {
193
209
this . _columnDefinitionsByName . set ( columnDef . name , columnDef ) ;
194
210
} ) ;
211
+ }
195
212
196
- this . _columnDefinitions . changes . subscribe ( ( ) => {
197
- console . log ( 'Column def change' ) ;
198
- } ) ;
199
-
200
- // Re-render the rows if any of their columns change.
201
- // TODO(andrewseguin): Determine how to only re-render the rows that have their columns changed.
202
- const columnChangeEvents = this . _rowDefinitions . map ( rowDef => rowDef . columnsChange ) ;
213
+ /**
214
+ * Check if the header or rows have changed what columns they want to display. If there is a diff,
215
+ * then re-render that section.
216
+ */
217
+ private _checkColumnsChange ( ) {
218
+ // Re-render the rows when the row definition columns change.
219
+ this . _rowDefinitions . forEach ( def => {
220
+ if ( ! ! def . getColumnsDiff ( ) ) {
221
+ // Reset the data to an empty array so that renderRowChanges will re-render all new rows.
222
+ this . _dataDiffer . diff ( [ ] ) ;
203
223
204
- takeUntil . call ( merge ( ...columnChangeEvents ) , this . _onDestroy ) . subscribe ( ( ) => {
205
- // Reset the data to an empty array so that renderRowChanges will re-render all new rows.
206
- this . _rowPlaceholder . viewContainer . clear ( ) ;
207
- this . _dataDiffer . diff ( [ ] ) ;
208
- this . _renderRowChanges ( ) ;
224
+ this . _rowPlaceholder . viewContainer . clear ( ) ;
225
+ this . _renderRowChanges ( ) ;
226
+ }
209
227
} ) ;
210
228
211
- // Re-render the header row if the columns change
212
- takeUntil . call ( this . _headerDefinition . columnsChange , this . _onDestroy ) . subscribe ( ( ) => {
213
- console . log ( 'Header columns changed' ) ;
229
+ // Re-render the header row if there is a difference in its columns.
230
+ if ( this . _headerDefinition . getColumnsDiff ( ) ) {
214
231
this . _headerRowPlaceholder . viewContainer . clear ( ) ;
215
232
this . _renderHeaderRow ( ) ;
216
- } ) ;
217
- }
218
-
219
- ngAfterViewInit ( ) {
220
- // Find and construct an iterable differ that can be used to find the diff in an array.
221
- this . _dataDiffer = this . _differs . find ( [ ] ) . create ( this . _trackByFn ) ;
222
- this . _isViewInitialized = true ;
223
- }
224
-
225
- ngDoCheck ( ) {
226
- if ( this . _isViewInitialized && this . dataSource && ! this . _renderChangeSubscription ) {
227
- this . _renderHeaderRow ( ) ;
228
- if ( this . dataSource && ! this . _renderChangeSubscription ) {
229
- this . _observeRenderChanges ( ) ;
230
- }
231
233
}
232
234
}
233
235
0 commit comments