File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,10 @@ export class CdkTable<T> implements CollectionViewer {
193
193
this . _columnDefinitionsByName . set ( columnDef . name , columnDef ) ;
194
194
} ) ;
195
195
196
+ this . _columnDefinitions . changes . subscribe ( ( ) => {
197
+ console . log ( 'Column def change' ) ;
198
+ } ) ;
199
+
196
200
// Re-render the rows if any of their columns change.
197
201
// TODO(andrewseguin): Determine how to only re-render the rows that have their columns changed.
198
202
const columnChangeEvents = this . _rowDefinitions . map ( rowDef => rowDef . columnsChange ) ;
@@ -206,6 +210,7 @@ export class CdkTable<T> implements CollectionViewer {
206
210
207
211
// Re-render the header row if the columns change
208
212
takeUntil . call ( this . _headerDefinition . columnsChange , this . _onDestroy ) . subscribe ( ( ) => {
213
+ console . log ( 'Header columns changed' ) ;
209
214
this . _headerRowPlaceholder . viewContainer . clear ( ) ;
210
215
this . _renderHeaderRow ( ) ;
211
216
} ) ;
Original file line number Diff line number Diff line change 25
25
< md-checkbox (change) ="toggleHighlight('even', $event.checked) "> Even Rows</ md-checkbox >
26
26
< md-checkbox (change) ="toggleHighlight('odd', $event.checked) "> Odd Rows</ md-checkbox >
27
27
</ div >
28
+
29
+ < div >
30
+ < button md-raised-button (click) ="addDynamicColumnDef() "> Add Dynamic Column Def</ button >
31
+ </ div >
28
32
</ div >
29
33
34
+ < h3 > CdkTable With Dynamic Column Def</ h3 >
35
+
36
+ < cdk-table [dataSource] ="dataSource ">
37
+ < ng-container cdkColumnDef ="column.id " *ngFor ="let column of dynamicColumns ">
38
+ < cdk-header-cell *cdkHeaderCellDef > {{column.headerText}} </ cdk-header-cell >
39
+ < cdk-cell *cdkCellDef ="let row "> {{row[column.property]}} </ cdk-cell >
40
+ </ ng-container >
41
+
42
+ < cdk-header-row *cdkHeaderRowDef ="dynamicColumnIds "> </ cdk-header-row >
43
+ < cdk-row *cdkRowDef ="let row; columns: dynamicColumnIds; "> </ cdk-row >
44
+ </ cdk-table >
45
+
30
46
< h3 > CdkTable Example</ h3 >
31
47
32
48
< cdk-table #table mdSort
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ export class TableDemo {
21
21
changeReferences = false ;
22
22
highlights = new Set < string > ( ) ;
23
23
24
+ dynamicColumns : any [ ] = [ ] ;
25
+ dynamicColumnIds : string [ ] = [ ] ;
26
+
24
27
@ViewChild ( MdPaginator ) _paginator : MdPaginator ;
25
28
26
29
@ViewChild ( MdSort ) sort : MdSort ;
@@ -31,6 +34,18 @@ export class TableDemo {
31
34
this . connect ( ) ;
32
35
}
33
36
37
+ addDynamicColumnDef ( ) {
38
+ const properties = [ 'userId' , 'userName' , 'progress' , 'color' ] ;
39
+ const nextProperty = properties [ this . dynamicColumns . length ] ;
40
+ this . dynamicColumns . push ( {
41
+ id : nextProperty ,
42
+ property : nextProperty ,
43
+ headerText : nextProperty
44
+ } ) ;
45
+
46
+ this . dynamicColumnIds . push ( nextProperty ) ;
47
+ }
48
+
34
49
connect ( ) {
35
50
this . displayedColumns = [ 'userId' , 'userName' , 'progress' , 'color' ] ;
36
51
this . dataSource = new PersonDataSource ( this . _peopleDatabase ,
You can’t perform that action at this time.
0 commit comments