Skip to content

fix(cdk/table): project colgroup and col elements #18135

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
Apr 8, 2020
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
44 changes: 44 additions & 0 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ describe('CdkTable', () => {
expect(tableElement.firstElementChild).toBe(caption);
}));

it('should be able to project colgroup and col', fakeAsync(() => {
setupTableTestApp(NativeHtmlTableWithColgroupAndCol);
fixture.detectChanges();

const colgroupsAndCols = Array.from(tableElement.querySelectorAll('colgroup, col'));

expect(colgroupsAndCols.length).toBe(3);
expect(colgroupsAndCols[0].childNodes[0]).toBe(colgroupsAndCols[1]);
expect(colgroupsAndCols[2].parentNode!.nodeName.toLowerCase()).toBe('table');
expect(colgroupsAndCols.map(e => e.nodeName.toLowerCase())).toEqual([
'colgroup',
'col',
'col',
]);
}));

describe('with different data inputs other than data source', () => {
let baseData: TestData[] = [
{a: 'a_1', b: 'b_1', c: 'c_1'},
Expand Down Expand Up @@ -2390,6 +2406,34 @@ class NativeHtmlTableWithCaptionApp {
@ViewChild(CdkTable) table: CdkTable<TestData>;
}

@Component({
template: `
<table cdk-table [dataSource]="dataSource">
<colgroup>
<col>
</colgroup>
<col>
<ng-container cdkColumnDef="column_a">
<th cdk-header-cell *cdkHeaderCellDef> Column A</th>
<td cdk-cell *cdkCellDef="let row"> {{row.a}}</td>
</ng-container>
<ng-container cdkColumnDef="column_b">
<th cdk-header-cell *cdkHeaderCellDef> Column B</th>
<td cdk-cell *cdkCellDef="let row"> {{row.b}}</td>
</ng-container>

<tr cdk-header-row *cdkHeaderRowDef="columnsToRender"></tr>
<tr cdk-row *cdkRowDef="let row; columns: columnsToRender" class="customRowClass"></tr>
</table>
`
})
class NativeHtmlTableWithColgroupAndCol {
dataSource: FakeDataSource | undefined = new FakeDataSource();
columnsToRender = ['column_a', 'column_b'];

@ViewChild(CdkTable) table: CdkTable<TestData>;
}

@Component({
// Note that we need the `ngSwitch` below in order to surface the issue we're testing for.
template: `
Expand Down
1 change: 1 addition & 0 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const CDK_TABLE_TEMPLATE =
// element in the table. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption
`
<ng-content select="caption"></ng-content>
<ng-content select="colgroup, col"></ng-content>
<ng-container headerRowOutlet></ng-container>
<ng-container rowOutlet></ng-container>
<ng-container footerRowOutlet></ng-container>
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/cdk/table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export declare const CDK_ROW_TEMPLATE = "<ng-container cdkCellOutlet></ng-contai

export declare const CDK_TABLE: InjectionToken<any>;

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";
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";

export declare class CdkCell extends BaseCdkCell {
constructor(columnDef: CdkColumnDef, elementRef: ElementRef);
Expand Down Expand Up @@ -206,7 +206,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
updateStickyFooterRowStyles(): void;
updateStickyHeaderRowStyles(): void;
static ngAcceptInputType_multiTemplateDataRows: BooleanInput;
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; }, {}, ["_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption"]>;
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"]>;
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null]>;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export declare class MatRowDef<T> extends CdkRowDef<T> {

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

Expand Down