Skip to content

fix(cdk/table): error if data is accessed too early #30817

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 7, 2025
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
2 changes: 1 addition & 1 deletion goldens/cdk/table/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export class CdkTable<T> implements AfterContentInit, AfterContentChecked, Colle
_contentFooterRowDefs: QueryList<CdkFooterRowDef>;
_contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
_contentRowDefs: QueryList<CdkRowDef<T>>;
protected _data: readonly T[];
protected _data: readonly T[] | undefined;
get dataSource(): CdkTableDataSourceInput<T>;
set dataSource(dataSource: CdkTableDataSourceInput<T>);
// (undocumented)
Expand Down
1 change: 1 addition & 0 deletions src/cdk/table/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ts_project(
deps = [
":table",
"//:node_modules/@angular/core",
"//:node_modules/@angular/platform-browser",
"//:node_modules/rxjs",
"//src/cdk/bidi",
"//src/cdk/collections",
Expand Down
10 changes: 10 additions & 0 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ViewChild,
inject,
} from '@angular/core';
import {By} from '@angular/platform-browser';
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
import {BehaviorSubject, Observable, combineLatest, of as observableOf} from 'rxjs';
import {map} from 'rxjs/operators';
Expand Down Expand Up @@ -376,6 +377,15 @@ describe('CdkTable', () => {
expect(colgroupsAndCols.map(e => e.nodeName.toLowerCase())).toEqual(['colgroup', 'col', 'col']);
}));

it('should not throw if `renderRows` is called too early', () => {
// Note that we don't call `detectChanges` here, because we're testing specifically
// what happens when `renderRows` is called before the first change detection run.
const fixture = createComponent(SimpleCdkTableApp);
const table = fixture.debugElement.query(By.directive(CdkTable))
.componentInstance as CdkTable<unknown>;
expect(() => table.renderRows()).not.toThrow();
});

describe('with different data inputs other than data source', () => {
let baseData: TestData[] = [
{a: 'a_1', b: 'b_1', c: 'c_1'},
Expand Down
14 changes: 9 additions & 5 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class CdkTable<T>
private _document = inject(DOCUMENT);

/** Latest data provided by the data source. */
protected _data: readonly T[];
protected _data: readonly T[] | undefined;

/** Subject that emits when the component has been destroyed. */
private readonly _onDestroy = new Subject<void>();
Expand Down Expand Up @@ -621,17 +621,17 @@ export class CdkTable<T>

this._isServer = !this._platform.isBrowser;
this._isNativeHtmlTable = this._elementRef.nativeElement.nodeName === 'TABLE';
}

ngOnInit() {
this._setupStickyStyler();

// Set up the trackBy function so that it uses the `RenderRow` as its identity by default. If
// the user has provided a custom trackBy, return the result of that function as evaluated
// with the values of the `RenderRow`'s data and index.
this._dataDiffer = this._differs.find([]).create((_i: number, dataRow: RenderRow<T>) => {
return this.trackBy ? this.trackBy(dataRow.dataIndex, dataRow.data) : dataRow;
});
}

ngOnInit() {
this._setupStickyStyler();

this._viewportRuler
.change()
Expand Down Expand Up @@ -981,6 +981,10 @@ export class CdkTable<T>
const prevCachedRenderRows = this._cachedRenderRowsMap;
this._cachedRenderRowsMap = new Map();

if (!this._data) {
return renderRows;
}

// For each data object, get the list of rows that should be rendered, represented by the
// respective `RenderRow` object which is the pair of `data` and `CdkRowDef`.
for (let i = 0; i < this._data.length; i++) {
Expand Down
Loading