Skip to content

Commit f601e83

Browse files
andrewseguinmmalerba
authored andcommitted
fix(table): render cells even if data is falsy (#7914)
* fix(table): render cells even if data is falsy * add test * test naming * fix all usages of evensimpler
1 parent 520d83b commit f601e83

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/cdk/table/table.spec.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ describe('CdkTable', () => {
3737
UndefinedColumnsCdkTableApp,
3838
WhenRowCdkTableApp,
3939
WhenRowWithoutDefaultCdkTableApp,
40-
WhenRowMultipleDefaultsCdkTableApp
40+
WhenRowMultipleDefaultsCdkTableApp,
41+
BooleanRowCdkTableApp
4142
],
4243
}).compileComponents();
4344
}));
@@ -106,6 +107,21 @@ describe('CdkTable', () => {
106107
});
107108
});
108109

110+
it('should render cells even if row data is falsy', () => {
111+
const booleanRowCdkTableAppFixture = TestBed.createComponent(BooleanRowCdkTableApp);
112+
const booleanRowCdkTableElement =
113+
booleanRowCdkTableAppFixture.nativeElement.querySelector('cdk-table');
114+
booleanRowCdkTableAppFixture.detectChanges();
115+
116+
expectTableToMatchContent(booleanRowCdkTableElement, [
117+
[''], // Header row
118+
['false'], // Data rows
119+
['true'],
120+
['false'],
121+
['true'],
122+
]);
123+
});
124+
109125
it('should be able to apply class-friendly css class names for the column cells', () => {
110126
const crazyColumnNameAppFixture = TestBed.createComponent(CrazyColumnNameCdkTableApp);
111127
const crazyColumnNameTableElement =
@@ -636,6 +652,16 @@ class FakeDataSource extends DataSource<TestData> {
636652
}
637653
}
638654

655+
class BooleanDataSource extends DataSource<boolean> {
656+
_dataChange = new BehaviorSubject<boolean[]>([false, true, false, true]);
657+
658+
connect(): Observable<boolean[]> {
659+
return this._dataChange;
660+
}
661+
662+
disconnect() { }
663+
}
664+
639665
@Component({
640666
template: `
641667
<cdk-table [dataSource]="dataSource">
@@ -668,6 +694,23 @@ class SimpleCdkTableApp {
668694
@ViewChild(CdkTable) table: CdkTable<TestData>;
669695
}
670696

697+
@Component({
698+
template: `
699+
<cdk-table [dataSource]="dataSource">
700+
<ng-container cdkColumnDef="column_a">
701+
<cdk-header-cell *cdkHeaderCellDef></cdk-header-cell>
702+
<cdk-cell *cdkCellDef="let data"> {{data}} </cdk-cell>
703+
</ng-container>
704+
705+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
706+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
707+
</cdk-table>
708+
`
709+
})
710+
class BooleanRowCdkTableApp {
711+
dataSource = new BooleanDataSource();
712+
}
713+
671714
@Component({
672715
template: `
673716
<cdk-table [dataSource]="dataSource">

src/cdk/table/table.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ export class CdkTable<T> implements CollectionViewer {
360360
// CdkCellOutlet was instantiated as a result of `createEmbeddedView`.
361361
this._rowPlaceholder.viewContainer.createEmbeddedView(row.template, context, index);
362362

363-
// Insert empty cells if there is no data to improve rendering time.
364-
const cells = rowData ? this._getCellTemplatesForRow(row) : [];
365-
366-
cells.forEach(cell => {
363+
this._getCellTemplatesForRow(row).forEach(cell => {
367364
CdkCellOutlet.mostRecentCellOutlet._viewContainer.createEmbeddedView(cell.template, context);
368365
});
369366

0 commit comments

Comments
 (0)