@@ -24,6 +24,7 @@ fdescribe('CdkTable', () => {
24
24
DynamicDataSourceCdkTableApp ,
25
25
CustomRoleCdkTableApp ,
26
26
TrackByCdkTableApp ,
27
+ DynamicColumnDefinitionsCdkTableApp ,
27
28
RowContextCdkTableApp ,
28
29
] ,
29
30
} ) . compileComponents ( ) ;
@@ -107,6 +108,16 @@ fdescribe('CdkTable', () => {
107
108
expect ( fixture . nativeElement . querySelector ( 'cdk-table' ) . getAttribute ( 'role' ) ) . toBe ( 'treegrid' ) ;
108
109
} ) ;
109
110
111
+ fit ( 'should be able to dynamically add/remove column definitions' , ( ) => {
112
+ const dynamicColumnDefFixture = TestBed . createComponent ( DynamicColumnDefinitionsCdkTableApp ) ;
113
+ dynamicColumnDefFixture . detectChanges ( ) ;
114
+
115
+ const dynamicColumnDefComp = dynamicColumnDefFixture . componentInstance ;
116
+ expect ( dynamicColumnDefComp . dynamicColumns . push ( 'columnA' ) ) ;
117
+
118
+ dynamicColumnDefFixture . detectChanges ( ) ;
119
+ } ) ;
120
+
110
121
it ( 'should re-render the rows when the data changes' , ( ) => {
111
122
dataSource . addData ( ) ;
112
123
fixture . detectChanges ( ) ;
@@ -590,34 +601,25 @@ class TrackByCdkTableApp {
590
601
@Component ( {
591
602
template : `
592
603
<cdk-table [dataSource]="dataSource">
593
- <ng-container [cdkColumnDef]="column.id " *ngFor="let column of dynamicColumnDefs ">
594
- <cdk-header-cell *cdkHeaderCellDef> {{column.headerText}} </cdk-header-cell>
595
- <cdk-cell *cdkCellDef="let row"> {{row[ column.property]}} </cdk-cell>
604
+ <ng-container [cdkColumnDef]="column" *ngFor="let column of dynamicColumns ">
605
+ <cdk-header-cell *cdkHeaderCellDef> {{column}} </cdk-header-cell>
606
+ <cdk-cell *cdkCellDef="let row"> {{column}} </cdk-cell>
596
607
</ng-container>
597
608
598
- <cdk-header-row *cdkHeaderRowDef="dynamicColumnIds "></cdk-header-row>
599
- <cdk-row *cdkRowDef="let row; columns: dynamicColumnIds ;"></cdk-row>
609
+ <cdk-header-row *cdkHeaderRowDef="dynamicColumns "></cdk-header-row>
610
+ <cdk-row *cdkRowDef="let row; columns: dynamicColumns ;"></cdk-row>
600
611
</cdk-table>
601
612
`
602
613
} )
603
614
class DynamicColumnDefinitionsCdkTableApp {
604
- dynamicColumnDefs : any [ ] = [ ] ;
605
- dynamicColumnIds : string [ ] = [ ] ;
615
+ dynamicColumns : any [ ] = [ ] ;
606
616
607
617
dataSource : FakeDataSource = new FakeDataSource ( ) ;
608
- columnsToRender = [ 'column_a' , 'column_b' ] ;
609
618
610
619
@ViewChild ( CdkTable ) table : CdkTable < TestData > ;
611
620
612
621
addDynamicColumnDef ( ) {
613
- const nextProperty = this . dynamicColumnDefs . length ;
614
- this . dynamicColumnDefs . push ( {
615
- id : nextProperty ,
616
- property : nextProperty ,
617
- headerText : nextProperty
618
- } ) ;
619
-
620
- this . dynamicColumnIds = this . dynamicColumnDefs . map ( columnDef => columnDef . id ) ;
622
+ this . dynamicColumns . push ( this . dynamicColumns . length ) ;
621
623
}
622
624
}
623
625
0 commit comments