Skip to content

Commit 6982054

Browse files
crisbetommalerba
authored andcommitted
fix(table): not picking up indirect descendant defs (#17340)
Fixes the table not picking descendants that aren't direct descendants. Currently this isn't a huge problem, however with Ivy it'll become an issue that'll prevent consumers from setting an `ngIf` around the defs. Fixes #17339.
1 parent 0d29f4d commit 6982054

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/cdk/table/table.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ describe('CdkTable', () => {
541541
.toThrowError(getTableUnknownColumnError('column_a').message);
542542
});
543543

544+
it('should pick up columns that are indirect descendants', () => {
545+
expect(() => createComponent(TableWithIndirectDescendantDefs).detectChanges()).not.toThrow();
546+
});
547+
544548
it('should throw an error if a column definition is requested but not defined after render',
545549
fakeAsync(() => {
546550
const columnDefinitionMissingAfterRenderFixture =
@@ -2322,6 +2326,26 @@ class NativeHtmlTableWithCaptionApp {
23222326
@ViewChild(CdkTable) table: CdkTable<TestData>;
23232327
}
23242328

2329+
@Component({
2330+
// Note that we need the `ngSwitch` below in order to surface the issue we're testing for.
2331+
template: `
2332+
<cdk-table [dataSource]="dataSource">
2333+
<ng-container [ngSwitch]="true">
2334+
<ng-container cdkColumnDef="column_a">
2335+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
2336+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
2337+
</ng-container>
2338+
</ng-container>
2339+
2340+
<cdk-header-row *cdkHeaderRowDef="['column_a']"></cdk-header-row>
2341+
<cdk-row *cdkRowDef="let row; columns: ['column_a']"></cdk-row>
2342+
</cdk-table>
2343+
`
2344+
})
2345+
class TableWithIndirectDescendantDefs {
2346+
dataSource = new FakeDataSource();
2347+
}
2348+
23252349
function getElements(element: Element, query: string): Element[] {
23262350
return [].slice.call(element.querySelectorAll(query));
23272351
}

src/cdk/table/table.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,20 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
375375
* The column definitions provided by the user that contain what the header, data, and footer
376376
* cells should render for each column.
377377
*/
378-
@ContentChildren(CdkColumnDef) _contentColumnDefs: QueryList<CdkColumnDef>;
378+
@ContentChildren(CdkColumnDef, {descendants: true}) _contentColumnDefs: QueryList<CdkColumnDef>;
379379

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

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

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

389393
constructor(
390394
protected readonly _differs: IterableDiffers,

0 commit comments

Comments
 (0)