Skip to content

Commit a8a6617

Browse files
crisbetommalerba
authored andcommittedFeb 4, 2019
fix(table): allow for a caption to be projected (#14965)
Fixes consumers not being allowed to project a caption into a CDK table. Fixes #14948.
1 parent ba6b405 commit a8a6617

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed
 

‎src/cdk/table/table.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ describe('CdkTable', () => {
327327
]);
328328
});
329329

330+
it('should be able to project a caption', fakeAsync(() => {
331+
setupTableTestApp(NativeHtmlTableWithCaptionApp);
332+
fixture.detectChanges();
333+
334+
const caption = tableElement.querySelector('caption');
335+
336+
expect(caption).toBeTruthy();
337+
expect(tableElement.firstElementChild).toBe(caption);
338+
}));
339+
330340
describe('with different data inputs other than data source', () => {
331341
let baseData: TestData[] = [
332342
{a: 'a_1', b: 'b_1', c: 'c_1'},
@@ -2182,6 +2192,27 @@ class NativeHtmlTableApp {
21822192
@ViewChild(CdkTable) table: CdkTable<TestData>;
21832193
}
21842194

2195+
@Component({
2196+
template: `
2197+
<table cdk-table [dataSource]="dataSource">
2198+
<caption>Very important data</caption>
2199+
<ng-container cdkColumnDef="column_a">
2200+
<th cdk-header-cell *cdkHeaderCellDef> Column A</th>
2201+
<td cdk-cell *cdkCellDef="let row"> {{row.a}}</td>
2202+
</ng-container>
2203+
2204+
<tr cdk-header-row *cdkHeaderRowDef="columnsToRender"></tr>
2205+
<tr cdk-row *cdkRowDef="let row; columns: columnsToRender" class="customRowClass"></tr>
2206+
</table>
2207+
`
2208+
})
2209+
class NativeHtmlTableWithCaptionApp {
2210+
dataSource: FakeDataSource | undefined = new FakeDataSource();
2211+
columnsToRender = ['column_a'];
2212+
2213+
@ViewChild(CdkTable) table: CdkTable<TestData>;
2214+
}
2215+
21852216
function getElements(element: Element, query: string): Element[] {
21862217
return [].slice.call(element.querySelectorAll(query));
21872218
}

‎src/cdk/table/table.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,15 @@ export class FooterRowOutlet implements RowOutlet {
106106
* material library.
107107
* @docs-private
108108
*/
109-
export const CDK_TABLE_TEMPLATE = `
109+
export const CDK_TABLE_TEMPLATE =
110+
// Note that according to MDN, the `caption` element has to be projected as the **first** element
111+
// in the table. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption
112+
`
113+
<ng-content select="caption"></ng-content>
110114
<ng-container headerRowOutlet></ng-container>
111115
<ng-container rowOutlet></ng-container>
112-
<ng-container footerRowOutlet></ng-container>`;
116+
<ng-container footerRowOutlet></ng-container>
117+
`;
113118

114119
/**
115120
* Interface used to conveniently type the possible context interfaces for the render row.

‎tools/public_api_guard/cdk/table.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export declare type CanStickCtor = Constructor<CanStick>;
3030

3131
export declare const CDK_ROW_TEMPLATE = "<ng-container cdkCellOutlet></ng-container>";
3232

33-
export declare const CDK_TABLE_TEMPLATE = "\n <ng-container headerRowOutlet></ng-container>\n <ng-container rowOutlet></ng-container>\n <ng-container footerRowOutlet></ng-container>";
33+
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";
3434

3535
export declare class CdkCell extends BaseCdkCell {
3636
constructor(columnDef: CdkColumnDef, elementRef: ElementRef);

0 commit comments

Comments
 (0)
Please sign in to comment.