Skip to content

Commit

Permalink
fix(table): no data row not shown if data source is empty on init (#1…
Browse files Browse the repository at this point in the history
…9994)

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.

(cherry picked from commit b3099c8)
  • Loading branch information
crisbeto authored and wagnermaciel committed Jul 21, 2020
1 parent 9939124 commit 392e384
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ describe('CdkTable', () => {
expect(tableElement.textContent!.trim()).not.toContain('No data');
});

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

it('should render no rows when the data is null', fakeAsync(() => {
Expand Down
1 change: 1 addition & 0 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
this._renderRows = this._getAllRenderRows();
const changes = this._dataDiffer.diff(this._renderRows);
if (!changes) {
this._updateNoDataRow();
return;
}

Expand Down
9 changes: 9 additions & 0 deletions src/material-experimental/mdc-table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ describe('MDC-based MatTable', () => {
expect(tbody.textContent.trim()).not.toContain('No data');
});

it('should show the no data row if there is no data on init', () => {
const fixture = TestBed.createComponent(MatTableApp);
fixture.componentInstance.dataSource!.data = [];
fixture.detectChanges();

const tbody = fixture.nativeElement.querySelector('tbody')!;
expect(tbody.textContent.trim()).toContain('No data');
});

});

it('should render with MatTableDataSource and sort', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ describe('MatTable', () => {
expect(table.textContent.trim()).not.toContain('No data');
});

it('should show the no data row if there is no data on init', () => {
const fixture = TestBed.createComponent(MatTableApp);
fixture.componentInstance.dataSource!.data = [];
fixture.detectChanges();

const table = fixture.nativeElement.querySelector('.mat-table')!;
expect(table.textContent.trim()).toContain('No data');
});

});

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

0 comments on commit 392e384

Please sign in to comment.