Skip to content

Commit b3099c8

Browse files
authored
fix(table): no data row not shown if data source is empty on init (#19994)
There's an early `return` in the function that renders all the rows, including the one that is shown for no data, if there were no changes to the data source. This was preventing the no data row from showing up if the data source starts off as empty. Fixes #19992.
1 parent a3a8490 commit b3099c8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/cdk/table/table.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ describe('CdkTable', () => {
301301
expect(tableElement.textContent!.trim()).not.toContain('No data');
302302
});
303303

304+
it('should show the no data row if there is no data on init', () => {
305+
fixture.destroy();
306+
fixture = TestBed.createComponent(SimpleCdkTableApp);
307+
fixture.componentInstance.dataSource.data = [];
308+
fixture.detectChanges();
309+
tableElement = fixture.nativeElement.querySelector('.cdk-table');
310+
expect(tableElement.textContent!.trim()).toContain('No data');
311+
});
304312
});
305313

306314
it('should render no rows when the data is null', fakeAsync(() => {

src/cdk/table/table.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
512512
this._renderRows = this._getAllRenderRows();
513513
const changes = this._dataDiffer.diff(this._renderRows);
514514
if (!changes) {
515+
this._updateNoDataRow();
515516
return;
516517
}
517518

src/material-experimental/mdc-table/table.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ describe('MDC-based MatTable', () => {
117117
expect(tbody.textContent.trim()).not.toContain('No data');
118118
});
119119

120+
it('should show the no data row if there is no data on init', () => {
121+
const fixture = TestBed.createComponent(MatTableApp);
122+
fixture.componentInstance.dataSource!.data = [];
123+
fixture.detectChanges();
124+
125+
const tbody = fixture.nativeElement.querySelector('tbody')!;
126+
expect(tbody.textContent.trim()).toContain('No data');
127+
});
128+
120129
});
121130

122131
it('should render with MatTableDataSource and sort', () => {

src/material/table/table.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ describe('MatTable', () => {
104104
expect(table.textContent.trim()).not.toContain('No data');
105105
});
106106

107+
it('should show the no data row if there is no data on init', () => {
108+
const fixture = TestBed.createComponent(MatTableApp);
109+
fixture.componentInstance.dataSource!.data = [];
110+
fixture.detectChanges();
111+
112+
const table = fixture.nativeElement.querySelector('.mat-table')!;
113+
expect(table.textContent.trim()).toContain('No data');
114+
});
115+
107116
});
108117

109118
it('should be able to render a table correctly with native elements', () => {

0 commit comments

Comments
 (0)