Skip to content

fix(table): not picking up indirect descendant defs #17340

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

Merged
merged 1 commit into from
Oct 25, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ describe('CdkTable', () => {
.toThrowError(getTableUnknownColumnError('column_a').message);
});

it('should pick up columns that are indirect descendants', () => {
expect(() => createComponent(TableWithIndirectDescendantDefs).detectChanges()).not.toThrow();
});

it('should throw an error if a column definition is requested but not defined after render',
fakeAsync(() => {
const columnDefinitionMissingAfterRenderFixture =
Expand Down Expand Up @@ -2322,6 +2326,26 @@ class NativeHtmlTableWithCaptionApp {
@ViewChild(CdkTable, {static: false}) table: CdkTable<TestData>;
}

@Component({
// Note that we need the `ngSwitch` below in order to surface the issue we're testing for.
template: `
<cdk-table [dataSource]="dataSource">
<ng-container [ngSwitch]="true">
<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>
</ng-container>

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

function getElements(element: Element, query: string): Element[] {
return [].slice.call(element.querySelectorAll(query));
}
Expand Down
12 changes: 8 additions & 4 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,20 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
* The column definitions provided by the user that contain what the header, data, and footer
* cells should render for each column.
*/
@ContentChildren(CdkColumnDef) _contentColumnDefs: QueryList<CdkColumnDef>;
@ContentChildren(CdkColumnDef, {descendants: true}) _contentColumnDefs: QueryList<CdkColumnDef>;
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this makes assumptions that we can't nest tables. AFAIK it's not something we support anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Right, based on the trick we use the get cell outlets after stamping out row views, we absolutely, 100% do not support nested tables.


/** Set of data row definitions that were provided to the table as content children. */
@ContentChildren(CdkRowDef) _contentRowDefs: QueryList<CdkRowDef<T>>;
@ContentChildren(CdkRowDef, {descendants: true}) _contentRowDefs: QueryList<CdkRowDef<T>>;

/** Set of header row definitions that were provided to the table as content children. */
@ContentChildren(CdkHeaderRowDef) _contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
@ContentChildren(CdkHeaderRowDef, {
descendants: true
}) _contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;

/** Set of footer row definitions that were provided to the table as content children. */
@ContentChildren(CdkFooterRowDef) _contentFooterRowDefs: QueryList<CdkFooterRowDef>;
@ContentChildren(CdkFooterRowDef, {
descendants: true
}) _contentFooterRowDefs: QueryList<CdkFooterRowDef>;

constructor(
protected readonly _differs: IterableDiffers,
Expand Down