Skip to content

Commit 5d95650

Browse files
committed
fix(material/table): style no data row properly
The "no data" row in the table goes through a different creation flow than the regular table rows which means that it can't get its CSS classes and ends up being unstyled. These changes add some logic to apply the appropriate classes. Fixes #22349.
1 parent f004728 commit 5d95650

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

goldens/cdk/table/index.api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ export class CdkHeaderRowDef extends BaseRowDef implements CanStick, OnChanges {
249249
export class CdkNoDataRow {
250250
constructor(...args: unknown[]);
251251
// (undocumented)
252-
_contentClassName: string;
252+
_cellClassNames: string[];
253+
// (undocumented)
254+
_cellSelector: string;
255+
// (undocumented)
256+
_contentClassNames: string[];
253257
// (undocumented)
254258
templateRef: TemplateRef<any>;
255259
// (undocumented)

goldens/material/table/index.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ export class MatHeaderRowDef extends CdkHeaderRowDef {
130130

131131
// @public
132132
export class MatNoDataRow extends CdkNoDataRow {
133+
constructor();
133134
// (undocumented)
134-
_contentClassName: string;
135+
_cellSelector: string;
135136
// (undocumented)
136137
static ɵdir: i0.ɵɵDirectiveDeclaration<MatNoDataRow, "ng-template[matNoDataRow]", never, {}, {}, never, never, true, never>;
137138
// (undocumented)

src/cdk/table/row.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ export class CdkRow {}
366366
export class CdkNoDataRow {
367367
templateRef = inject<TemplateRef<any>>(TemplateRef);
368368

369-
_contentClassName = 'cdk-no-data-row';
369+
_contentClassNames = ['cdk-no-data-row', 'cdk-row'];
370+
_cellClassNames = ['cdk-cell'];
371+
_cellSelector = 'td, cdk-cell, [cdk-cell], .cdk-cell';
370372

371373
constructor(...args: unknown[]);
372374
constructor() {}

src/cdk/table/table.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,13 @@ export class CdkTable<T>
14191419
// to figure out which one to add it to when there are multiple.
14201420
if (view.rootNodes.length === 1 && rootNode?.nodeType === this._document.ELEMENT_NODE) {
14211421
rootNode.setAttribute('role', 'row');
1422-
rootNode.classList.add(noDataRow._contentClassName);
1422+
rootNode.classList.add(...noDataRow._contentClassNames);
1423+
1424+
const cells = rootNode.querySelectorAll(noDataRow._cellSelector);
1425+
1426+
for (let i = 0; i < cells.length; i++) {
1427+
cells[i].classList.add(...noDataRow._cellClassNames);
1428+
}
14231429
}
14241430
} else {
14251431
container.clear();

src/material/table/row.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,11 @@ export class MatRow extends CdkRow {}
130130
providers: [{provide: CdkNoDataRow, useExisting: MatNoDataRow}],
131131
})
132132
export class MatNoDataRow extends CdkNoDataRow {
133-
override _contentClassName = 'mat-mdc-no-data-row';
133+
override _cellSelector = 'td, mat-cell, [mat-cell], .mat-cell';
134+
135+
constructor() {
136+
super();
137+
this._contentClassNames.push('mat-mdc-no-data-row', 'mat-mdc-row', 'mdc-data-table__row');
138+
this._cellClassNames.push('mat-mdc-cell', 'mdc-data-table__cell');
139+
}
134140
}

0 commit comments

Comments
 (0)