Skip to content

Commit 30a7dc5

Browse files
committed
fix(table): error if row definition is on an ng-container
Fixes the table throwing an error if the row definitions are on an `ng-container`. Fixes #12460.
1 parent f5f7f1c commit 30a7dc5

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/cdk/table/sticky-styler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ export class StickyStyler {
4545
*/
4646
clearStickyPositioning(rows: HTMLElement[], stickyDirections: StickyDirection[]) {
4747
for (const row of rows) {
48+
// If the row isn't an element (e.g. if it's an `ng-container`),
49+
// it won't have inline styles or `children` so we skip it.
50+
if (row.nodeType !== row.ELEMENT_NODE) {
51+
continue;
52+
}
53+
4854
this._removeStickyStyle(row, stickyDirections);
55+
4956
for (let i = 0; i < row.children.length; i++) {
5057
const cell = row.children[i] as HTMLElement;
5158
this._removeStickyStyle(cell, stickyDirections);

src/lib/table/table.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import {DataSource} from '@angular/cdk/collections';
22
import {Component, OnInit, ViewChild} from '@angular/core';
3-
import {async, ComponentFixture, fakeAsync, flushMicrotasks, TestBed} from '@angular/core/testing';
3+
import {
4+
async,
5+
ComponentFixture,
6+
fakeAsync,
7+
flushMicrotasks,
8+
TestBed,
9+
tick,
10+
} from '@angular/core/testing';
411
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
512
import {BehaviorSubject, Observable} from 'rxjs';
613
import {MatPaginator, MatPaginatorModule} from '../paginator/index';
@@ -22,6 +29,7 @@ describe('MatTable', () => {
2229
MatTableWithSortApp,
2330
MatTableWithPaginatorApp,
2431
StickyTableApp,
32+
TableWithNgContainerRow,
2533
],
2634
}).compileComponents();
2735
}));
@@ -127,6 +135,16 @@ describe('MatTable', () => {
127135
expect(stuckCellElement.classList).toContain('mat-table-sticky');
128136
});
129137

138+
// Note: needs to be fakeAsync so it catches the error.
139+
it('should not throw when a row definition is on an ng-container', fakeAsync(() => {
140+
const fixture = TestBed.createComponent(TableWithNgContainerRow);
141+
142+
expect(() => {
143+
fixture.detectChanges();
144+
tick();
145+
}).not.toThrow();
146+
}));
147+
130148
describe('with MatTableDataSource and sort/pagination/filter', () => {
131149
let tableElement: HTMLElement;
132150
let fixture: ComponentFixture<ArrayDataSourceMatTableApp>;
@@ -750,6 +768,25 @@ class MatTableWithPaginatorApp implements OnInit {
750768
}
751769
}
752770

771+
@Component({
772+
template: `
773+
<mat-table [dataSource]="dataSource">
774+
<ng-container matColumnDef="column_a">
775+
<mat-header-cell *matHeaderCellDef>Column A</mat-header-cell>
776+
<mat-cell *matCellDef="let row">{{row.a}}</mat-cell>
777+
</ng-container>
778+
779+
<mat-header-row *matHeaderRowDef="columnsToRender"></mat-header-row>
780+
<ng-container *matRowDef="let row; columns: columnsToRender"></ng-container>
781+
</mat-table>
782+
`
783+
})
784+
class TableWithNgContainerRow {
785+
dataSource: FakeDataSource | null = new FakeDataSource();
786+
columnsToRender = ['column_a'];
787+
}
788+
789+
753790
function getElements(element: Element, query: string): Element[] {
754791
return [].slice.call(element.querySelectorAll(query));
755792
}

0 commit comments

Comments
 (0)