Skip to content

Commit 3ddf65b

Browse files
andrewseguinjelbourn
authored andcommitted
fix(table): gracefully handle undefined/null columns (#6862)
1 parent 1cfce8d commit 3ddf65b

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/cdk/table/row.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export abstract class BaseRowDef {
4242

4343
ngOnChanges(changes: SimpleChanges): void {
4444
// Create a new columns differ if one does not yet exist. Initialize it based on initial value
45-
// of the columns property.
46-
const columns = changes['columns'].currentValue;
45+
// of the columns property or an empty array if none is provided.
46+
const columns = changes['columns'].currentValue || [];
4747
if (!this._columnsDiffer && columns) {
4848
this._columnsDiffer = this._differs.find(columns).create();
4949
this._columnsDiffer.diff(columns);

src/cdk/table/table.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('CdkTable', () => {
3030
DuplicateColumnDefNameCdkTableApp,
3131
MissingColumnDefCdkTableApp,
3232
CrazyColumnNameCdkTableApp,
33+
UndefinedColumnsCdkTableApp,
3334
],
3435
}).compileComponents();
3536
}));
@@ -133,6 +134,21 @@ describe('CdkTable', () => {
133134
.toThrowError(getTableUnknownColumnError('column_a').message);
134135
});
135136

137+
it('should not throw an error if columns are undefined on initialization', () => {
138+
const undefinedColumnsFixture = TestBed.createComponent(UndefinedColumnsCdkTableApp);
139+
undefinedColumnsFixture.detectChanges();
140+
141+
tableElement = undefinedColumnsFixture.nativeElement.querySelector('cdk-table');
142+
143+
expect(getHeaderRow(tableElement)).toBeNull('Should be no header without cells');
144+
145+
// Rows should be empty since there are no columns to display.
146+
const rows = getRows(tableElement);
147+
expect(rows[0].textContent).toBe('');
148+
expect(rows[1].textContent).toBe('');
149+
expect(rows[2].textContent).toBe('');
150+
});
151+
136152
it('should be able to dynamically add/remove column definitions', () => {
137153
const dynamicColumnDefFixture = TestBed.createComponent(DynamicColumnDefinitionsCdkTableApp);
138154
dynamicColumnDefFixture.detectChanges();
@@ -753,6 +769,24 @@ class MissingColumnDefCdkTableApp {
753769
dataSource: FakeDataSource = new FakeDataSource();
754770
}
755771

772+
@Component({
773+
template: `
774+
<cdk-table [dataSource]="dataSource">
775+
<ng-container cdkColumnDef="column_a">
776+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
777+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
778+
</ng-container>
779+
780+
<cdk-header-row *cdkHeaderRowDef="undefinedColumns"></cdk-header-row>
781+
<cdk-row *cdkRowDef="let row; columns: undefinedColumns"></cdk-row>
782+
</cdk-table>
783+
`
784+
})
785+
class UndefinedColumnsCdkTableApp {
786+
undefinedColumns;
787+
dataSource: FakeDataSource = new FakeDataSource();
788+
}
789+
756790
@Component({
757791
template: `
758792
<cdk-table [dataSource]="dataSource">

0 commit comments

Comments
 (0)