Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): render cells even if data is falsy #7914

Merged
merged 4 commits into from Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/cdk/table/table.spec.ts
Expand Up @@ -38,7 +38,8 @@ describe('CdkTable', () => {
UndefinedColumnsCdkTableApp,
WhenRowCdkTableApp,
WhenRowWithoutDefaultCdkTableApp,
WhenRowMultipleDefaultsCdkTableApp
WhenRowMultipleDefaultsCdkTableApp,
BooleanRowCdkTableApp
],
}).compileComponents();
}));
Expand Down Expand Up @@ -107,6 +108,21 @@ describe('CdkTable', () => {
});
});

it('should render cells even if row data is falsy', () => {
const booleanRowCdkTableAppFixture = TestBed.createComponent(BooleanRowCdkTableApp);
const booleanRowCdkTableElement =
booleanRowCdkTableAppFixture.nativeElement.querySelector('cdk-table');
booleanRowCdkTableAppFixture.detectChanges();

expectTableToMatchContent(booleanRowCdkTableElement, [
[''], // Header row
['false'], // Data rows
['true'],
['false'],
['true'],
]);
});

it('should be able to apply class-friendly css class names for the column cells', () => {
const crazyColumnNameAppFixture = TestBed.createComponent(CrazyColumnNameCdkTableApp);
const crazyColumnNameTableElement =
Expand Down Expand Up @@ -619,6 +635,16 @@ class FakeDataSource extends DataSource<TestData> {
}
}

class BooleanDataSource extends DataSource<boolean> {
_dataChange = new BehaviorSubject<boolean[]>([false, true, false, true]);

connect(): Observable<boolean[]> {
return this._dataChange;
}

disconnect() { }
}

@Component({
template: `
<cdk-table [dataSource]="dataSource">
Expand Down Expand Up @@ -651,6 +677,23 @@ class SimpleCdkTableApp {
@ViewChild(CdkTable) table: CdkTable<TestData>;
}

@Component({
template: `
<cdk-table [dataSource]="dataSource">
<ng-container cdkColumnDef="column_a">
<cdk-header-cell *cdkHeaderCellDef></cdk-header-cell>
<cdk-cell *cdkCellDef="let data"> {{data}} </cdk-cell>
</ng-container>

<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
</cdk-table>
`
})
class BooleanRowCdkTableApp {
dataSource = new BooleanDataSource();
}

@Component({
template: `
<cdk-table [dataSource]="dataSource">
Expand Down
5 changes: 1 addition & 4 deletions src/cdk/table/table.ts
Expand Up @@ -342,10 +342,7 @@ export class CdkTable<T> implements CollectionViewer {
// CdkCellOutlet was instantiated as a result of `createEmbeddedView`.
this._rowPlaceholder.viewContainer.createEmbeddedView(row.template, context, index);

// Insert empty cells if there is no data to improve rendering time.
const cells = rowData ? this._getCellTemplatesForRow(row) : [];

cells.forEach(cell => {
this._getCellTemplatesForRow(row).forEach(cell => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a unit test

CdkCellOutlet.mostRecentCellOutlet._viewContainer.createEmbeddedView(cell.template, context);
});

Expand Down