Skip to content

Commit ef57919

Browse files
crisbetojelbourn
authored andcommitted
fix(table): error if row definition is on an ng-container (#12462)
Fixes the table throwing an error if the row definitions are on an `ng-container`. Fixes #12460.
1 parent 170665a commit ef57919

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Diff for: src/cdk/table/sticky-styler.ts

+7
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);

Diff for: src/lib/table/table.spec.ts

+40-1
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,27 @@ 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">
781+
<mat-row></mat-row>
782+
</ng-container>
783+
</mat-table>
784+
`
785+
})
786+
class TableWithNgContainerRow {
787+
dataSource: FakeDataSource | null = new FakeDataSource();
788+
columnsToRender = ['column_a'];
789+
}
790+
791+
753792
function getElements(element: Element, query: string): Element[] {
754793
return [].slice.call(element.querySelectorAll(query));
755794
}

0 commit comments

Comments
 (0)