fix(table): gracefully handle undefined/null columns (#6862)

This commit is contained in:
Andrew Seguin
2017-09-20 16:05:34 -07:00
committed by Jeremy Elbourn
parent 1cfce8d9ab
commit 3ddf65b72f
2 changed files with 36 additions and 2 deletions
+2 -2
View File
@@ -42,8 +42,8 @@ export abstract class BaseRowDef {
ngOnChanges(changes: SimpleChanges): void {
// Create a new columns differ if one does not yet exist. Initialize it based on initial value
// of the columns property.
const columns = changes['columns'].currentValue;
// of the columns property or an empty array if none is provided.
const columns = changes['columns'].currentValue || [];
if (!this._columnsDiffer && columns) {
this._columnsDiffer = this._differs.find(columns).create();
this._columnsDiffer.diff(columns);
+34
View File
@@ -30,6 +30,7 @@ describe('CdkTable', () => {
DuplicateColumnDefNameCdkTableApp,
MissingColumnDefCdkTableApp,
CrazyColumnNameCdkTableApp,
UndefinedColumnsCdkTableApp,
],
}).compileComponents();
}));
@@ -133,6 +134,21 @@ describe('CdkTable', () => {
.toThrowError(getTableUnknownColumnError('column_a').message);
});
it('should not throw an error if columns are undefined on initialization', () => {
const undefinedColumnsFixture = TestBed.createComponent(UndefinedColumnsCdkTableApp);
undefinedColumnsFixture.detectChanges();
tableElement = undefinedColumnsFixture.nativeElement.querySelector('cdk-table');
expect(getHeaderRow(tableElement)).toBeNull('Should be no header without cells');
// Rows should be empty since there are no columns to display.
const rows = getRows(tableElement);
expect(rows[0].textContent).toBe('');
expect(rows[1].textContent).toBe('');
expect(rows[2].textContent).toBe('');
});
it('should be able to dynamically add/remove column definitions', () => {
const dynamicColumnDefFixture = TestBed.createComponent(DynamicColumnDefinitionsCdkTableApp);
dynamicColumnDefFixture.detectChanges();
@@ -753,6 +769,24 @@ class MissingColumnDefCdkTableApp {
dataSource: FakeDataSource = new FakeDataSource();
}
@Component({
template: `
<cdk-table [dataSource]="dataSource">
<ng-container cdkColumnDef="column_a">
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
</ng-container>
<cdk-header-row *cdkHeaderRowDef="undefinedColumns"></cdk-header-row>
<cdk-row *cdkRowDef="let row; columns: undefinedColumns"></cdk-row>
</cdk-table>
`
})
class UndefinedColumnsCdkTableApp {
undefinedColumns;
dataSource: FakeDataSource = new FakeDataSource();
}
@Component({
template: `
<cdk-table [dataSource]="dataSource">