Skip to content

Commit 2cca961

Browse files
committed
fix(table): Measure column width for sticky columns after new data has rendered.
1 parent a801603 commit 2cca961

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/cdk/table/table.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ describe('CdkTable', () => {
353353
it('should be able to project a caption', fakeAsync(() => {
354354
setupTableTestApp(NativeHtmlTableWithCaptionApp);
355355
fixture.detectChanges();
356+
flush();
356357

357358
const caption = tableElement.querySelector('caption');
358359

@@ -363,6 +364,7 @@ describe('CdkTable', () => {
363364
it('should be able to project colgroup and col', fakeAsync(() => {
364365
setupTableTestApp(NativeHtmlTableWithColgroupAndCol);
365366
fixture.detectChanges();
367+
flush();
366368

367369
const colgroupsAndCols = Array.from(tableElement.querySelectorAll('colgroup, col'));
368370

src/cdk/table/table.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
IterableChangeRecord,
4141
IterableDiffer,
4242
IterableDiffers,
43+
NgZone,
4344
OnDestroy,
4445
OnInit,
4546
Optional,
@@ -525,6 +526,12 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
525526
@SkipSelf()
526527
@Inject(STICKY_POSITIONING_LISTENER)
527528
protected readonly _stickyPositioningListener: StickyPositioningListener,
529+
/**
530+
* @deprecated `_ngZone` parameter to become required.
531+
* @breaking-change 14.0.0
532+
*/
533+
@Optional()
534+
protected readonly _ngZone: NgZone,
528535
) {
529536
if (!role) {
530537
this._elementRef.nativeElement.setAttribute('role', 'table');
@@ -666,7 +673,20 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
666673
});
667674

668675
this._updateNoDataRow();
669-
this.updateStickyColumnStyles();
676+
677+
// Allow the new row data to render before measuring it.
678+
// TODO: Remove indirection once _ngZone is required.
679+
const deferredUpdateStickyColumnStyles = () => {
680+
setTimeout(() => {
681+
this.updateStickyColumnStyles();
682+
}, 0);
683+
};
684+
685+
if (this._ngZone) {
686+
this._ngZone.runOutsideAngular(deferredUpdateStickyColumnStyles);
687+
} else {
688+
deferredUpdateStickyColumnStyles();
689+
}
670690

671691
this.contentChanged.next();
672692
}

0 commit comments

Comments
 (0)