Skip to content

Commit 2782273

Browse files
CaptainCow95mmalerba
authored andcommitted
fix(table): Clarify unknown table column error (#14947)
Throwing error when column not found in _addStickyColumnStyles to avoid weird error of "Cannot read property 'sticky' of undefined".
1 parent 4d5c5d5 commit 2782273

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/cdk/table/table.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
Input,
88
QueryList,
99
Type,
10-
ViewChild
10+
ViewChild,
11+
AfterViewInit
1112
} from '@angular/core';
1213
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
1314
import {BehaviorSubject, combineLatest, Observable, of as observableOf} from 'rxjs';
@@ -538,6 +539,17 @@ describe('CdkTable', () => {
538539
.toThrowError(getTableUnknownColumnError('column_a').message);
539540
});
540541

542+
it('should throw an error if a column definition is requested but not defined after render',
543+
fakeAsync(() => {
544+
const columnDefinitionMissingAfterRenderFixture =
545+
createComponent(MissingColumnDefAfterRenderCdkTableApp);
546+
expect(() => {
547+
columnDefinitionMissingAfterRenderFixture.detectChanges();
548+
flush();
549+
columnDefinitionMissingAfterRenderFixture.detectChanges();
550+
}).toThrowError(getTableUnknownColumnError('column_a').message);
551+
}));
552+
541553
it('should throw an error if the row definitions are missing', () => {
542554
expect(() => createComponent(MissingAllRowDefsCdkTableApp).detectChanges())
543555
.toThrowError(getTableMissingRowDefsError().message);
@@ -1974,6 +1986,28 @@ class MissingColumnDefCdkTableApp {
19741986
dataSource: FakeDataSource = new FakeDataSource();
19751987
}
19761988

1989+
@Component({
1990+
template: `
1991+
<cdk-table [dataSource]="dataSource">
1992+
<ng-container cdkColumnDef="column_b">
1993+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
1994+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
1995+
</ng-container>
1996+
1997+
<cdk-header-row *cdkHeaderRowDef="displayedColumns"></cdk-header-row>
1998+
<cdk-row *cdkRowDef="let row; columns: displayedColumns"></cdk-row>
1999+
</cdk-table>
2000+
`
2001+
})
2002+
class MissingColumnDefAfterRenderCdkTableApp implements AfterViewInit {
2003+
dataSource: FakeDataSource|null = null;
2004+
displayedColumns: string[] = [];
2005+
2006+
ngAfterViewInit() {
2007+
setTimeout(() => { this.displayedColumns = ['column_a']; }, 0);
2008+
}
2009+
}
2010+
19772011
@Component({
19782012
template: `
19792013
<cdk-table [dataSource]="dataSource">

src/cdk/table/table.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,13 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
852852

853853
/** Adds the sticky column styles for the rows according to the columns' stick states. */
854854
private _addStickyColumnStyles(rows: HTMLElement[], rowDef: BaseRowDef) {
855-
const columnDefs = Array.from(rowDef.columns || []).map(c => this._columnDefsByName.get(c)!);
855+
const columnDefs = Array.from(rowDef.columns || []).map(columnName => {
856+
const columnDef = this._columnDefsByName.get(columnName);
857+
if (!columnDef) {
858+
throw getTableUnknownColumnError(columnName);
859+
}
860+
return columnDef!;
861+
});
856862
const stickyStartStates = columnDefs.map(columnDef => columnDef.sticky);
857863
const stickyEndStates = columnDefs.map(columnDef => columnDef.stickyEnd);
858864
this._stickyStyler.updateStickyColumns(rows, stickyStartStates, stickyEndStates);

0 commit comments

Comments
 (0)