Skip to content

Commit 5b23084

Browse files
authored
fix(cdk/table): project colgroup and col elements (#18135)
1 parent 77730e9 commit 5b23084

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/cdk/table/table.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ describe('CdkTable', () => {
316316
expect(tableElement.firstElementChild).toBe(caption);
317317
}));
318318

319+
it('should be able to project colgroup and col', fakeAsync(() => {
320+
setupTableTestApp(NativeHtmlTableWithColgroupAndCol);
321+
fixture.detectChanges();
322+
323+
const colgroupsAndCols = Array.from(tableElement.querySelectorAll('colgroup, col'));
324+
325+
expect(colgroupsAndCols.length).toBe(3);
326+
expect(colgroupsAndCols[0].childNodes[0]).toBe(colgroupsAndCols[1]);
327+
expect(colgroupsAndCols[2].parentNode!.nodeName.toLowerCase()).toBe('table');
328+
expect(colgroupsAndCols.map(e => e.nodeName.toLowerCase())).toEqual([
329+
'colgroup',
330+
'col',
331+
'col',
332+
]);
333+
}));
334+
319335
describe('with different data inputs other than data source', () => {
320336
let baseData: TestData[] = [
321337
{a: 'a_1', b: 'b_1', c: 'c_1'},
@@ -2390,6 +2406,34 @@ class NativeHtmlTableWithCaptionApp {
23902406
@ViewChild(CdkTable) table: CdkTable<TestData>;
23912407
}
23922408

2409+
@Component({
2410+
template: `
2411+
<table cdk-table [dataSource]="dataSource">
2412+
<colgroup>
2413+
<col>
2414+
</colgroup>
2415+
<col>
2416+
<ng-container cdkColumnDef="column_a">
2417+
<th cdk-header-cell *cdkHeaderCellDef> Column A</th>
2418+
<td cdk-cell *cdkCellDef="let row"> {{row.a}}</td>
2419+
</ng-container>
2420+
<ng-container cdkColumnDef="column_b">
2421+
<th cdk-header-cell *cdkHeaderCellDef> Column B</th>
2422+
<td cdk-cell *cdkCellDef="let row"> {{row.b}}</td>
2423+
</ng-container>
2424+
2425+
<tr cdk-header-row *cdkHeaderRowDef="columnsToRender"></tr>
2426+
<tr cdk-row *cdkRowDef="let row; columns: columnsToRender" class="customRowClass"></tr>
2427+
</table>
2428+
`
2429+
})
2430+
class NativeHtmlTableWithColgroupAndCol {
2431+
dataSource: FakeDataSource | undefined = new FakeDataSource();
2432+
columnsToRender = ['column_a', 'column_b'];
2433+
2434+
@ViewChild(CdkTable) table: CdkTable<TestData>;
2435+
}
2436+
23932437
@Component({
23942438
// Note that we need the `ngSwitch` below in order to surface the issue we're testing for.
23952439
template: `

src/cdk/table/table.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export const CDK_TABLE_TEMPLATE =
116116
// element in the table. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption
117117
`
118118
<ng-content select="caption"></ng-content>
119+
<ng-content select="colgroup, col"></ng-content>
119120
<ng-container headerRowOutlet></ng-container>
120121
<ng-container rowOutlet></ng-container>
121122
<ng-container footerRowOutlet></ng-container>

tools/public_api_guard/cdk/table.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export declare const CDK_ROW_TEMPLATE = "<ng-container cdkCellOutlet></ng-contai
2727

2828
export declare const CDK_TABLE: InjectionToken<any>;
2929

30-
export declare const CDK_TABLE_TEMPLATE = "\n <ng-content select=\"caption\"></ng-content>\n <ng-container headerRowOutlet></ng-container>\n <ng-container rowOutlet></ng-container>\n <ng-container footerRowOutlet></ng-container>\n";
30+
export declare const CDK_TABLE_TEMPLATE = "\n <ng-content select=\"caption\"></ng-content>\n <ng-content select=\"colgroup, col\"></ng-content>\n <ng-container headerRowOutlet></ng-container>\n <ng-container rowOutlet></ng-container>\n <ng-container footerRowOutlet></ng-container>\n";
3131

3232
export declare class CdkCell extends BaseCdkCell {
3333
constructor(columnDef: CdkColumnDef, elementRef: ElementRef);
@@ -206,7 +206,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
206206
updateStickyFooterRowStyles(): void;
207207
updateStickyHeaderRowStyles(): void;
208208
static ngAcceptInputType_multiTemplateDataRows: BooleanInput;
209-
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; }, {}, ["_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption"]>;
209+
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; }, {}, ["_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption", "colgroup, col"]>;
210210
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null]>;
211211
}
212212

tools/public_api_guard/material/table.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export declare class MatRowDef<T> extends CdkRowDef<T> {
7272

7373
export declare class MatTable<T> extends CdkTable<T> {
7474
protected stickyCssClass: string;
75-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatTable<any>, "mat-table, table[mat-table]", ["matTable"], {}, {}, never, ["caption"]>;
75+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatTable<any>, "mat-table, table[mat-table]", ["matTable"], {}, {}, never, ["caption", "colgroup, col"]>;
7676
static ɵfac: i0.ɵɵFactoryDef<MatTable<any>, never>;
7777
}
7878

0 commit comments

Comments
 (0)