Skip to content

Commit 49c4dac

Browse files
committed
fix(cdk/table): project colgroup and col elements
1 parent 2fd862d commit 49c4dac

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
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'},
@@ -2326,6 +2342,34 @@ class NativeHtmlTableWithCaptionApp {
23262342
@ViewChild(CdkTable) table: CdkTable<TestData>;
23272343
}
23282344

2345+
@Component({
2346+
template: `
2347+
<table cdk-table [dataSource]="dataSource">
2348+
<colgroup>
2349+
<col>
2350+
</colgroup>
2351+
<col>
2352+
<ng-container cdkColumnDef="column_a">
2353+
<th cdk-header-cell *cdkHeaderCellDef> Column A</th>
2354+
<td cdk-cell *cdkCellDef="let row"> {{row.a}}</td>
2355+
</ng-container>
2356+
<ng-container cdkColumnDef="column_b">
2357+
<th cdk-header-cell *cdkHeaderCellDef> Column B</th>
2358+
<td cdk-cell *cdkCellDef="let row"> {{row.b}}</td>
2359+
</ng-container>
2360+
2361+
<tr cdk-header-row *cdkHeaderRowDef="columnsToRender"></tr>
2362+
<tr cdk-row *cdkRowDef="let row; columns: columnsToRender" class="customRowClass"></tr>
2363+
</table>
2364+
`
2365+
})
2366+
class NativeHtmlTableWithColgroupAndCol {
2367+
dataSource: FakeDataSource | undefined = new FakeDataSource();
2368+
columnsToRender = ['column_a', 'column_b'];
2369+
2370+
@ViewChild(CdkTable) table: CdkTable<TestData>;
2371+
}
2372+
23292373
@Component({
23302374
// Note that we need the `ngSwitch` below in order to surface the issue we're testing for.
23312375
template: `

src/cdk/table/table.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export const CDK_TABLE_TEMPLATE =
108108
// element in the table. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption
109109
`
110110
<ng-content select="caption"></ng-content>
111+
<ng-content select="colgroup, col"></ng-content>
111112
<ng-container headerRowOutlet></ng-container>
112113
<ng-container rowOutlet></ng-container>
113114
<ng-container footerRowOutlet></ng-container>

0 commit comments

Comments
 (0)