Skip to content

Commit db5d8ce

Browse files
kseamonwagnermaciel
authored andcommitted
fix(cdk/table): Measure column width for sticky columns after new data has rendered. (#23885)
(cherry picked from commit 853841c)
1 parent 84e6b02 commit db5d8ce

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/cdk/table/table.ts

+18-2
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,
@@ -60,7 +61,7 @@ import {
6061
Subject,
6162
Subscription,
6263
} from 'rxjs';
63-
import {takeUntil} from 'rxjs/operators';
64+
import {take, takeUntil} from 'rxjs/operators';
6465
import {CdkColumnDef} from './cell';
6566
import {_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER} from './coalesced-style-scheduler';
6667
import {
@@ -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,16 @@ 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+
// @breaking-change 14.0.0 Remove undefined check once _ngZone is required.
679+
if (this._ngZone && NgZone.isInAngularZone()) {
680+
this._ngZone.onStable.pipe(take(1), takeUntil(this._onDestroy)).subscribe(() => {
681+
this.updateStickyColumnStyles();
682+
});
683+
} else {
684+
this.updateStickyColumnStyles();
685+
}
670686

671687
this.contentChanged.next();
672688
}

tools/public_api_guard/cdk/table.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ export class CdkRowDef<T> extends BaseRowDef {
289289
// @public
290290
export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
291291
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform, _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>, _coalescedStyleScheduler: _CoalescedStyleScheduler, _viewportRuler: ViewportRuler,
292-
_stickyPositioningListener: StickyPositioningListener);
292+
_stickyPositioningListener: StickyPositioningListener,
293+
_ngZone: NgZone);
293294
addColumnDef(columnDef: CdkColumnDef): void;
294295
addFooterRowDef(footerRowDef: CdkFooterRowDef): void;
295296
addHeaderRowDef(headerRowDef: CdkHeaderRowDef): void;
@@ -332,6 +333,8 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
332333
ngOnDestroy(): void;
333334
// (undocumented)
334335
ngOnInit(): void;
336+
// @deprecated (undocumented)
337+
protected readonly _ngZone: NgZone;
335338
_noDataRow: CdkNoDataRow;
336339
// (undocumented)
337340
_noDataRowOutlet: NoDataRowOutlet;
@@ -360,7 +363,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
360363
// (undocumented)
361364
static ɵcmp: i0.ɵɵComponentDeclaration<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; "fixedLayout": "fixedLayout"; }, { "contentChanged": "contentChanged"; }, ["_noDataRow", "_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption", "colgroup, col"]>;
362365
// (undocumented)
363-
static ɵfac: i0.ɵɵFactoryDeclaration<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, null, null, null, { optional: true; skipSelf: true; }]>;
366+
static ɵfac: i0.ɵɵFactoryDeclaration<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, null, null, null, { optional: true; skipSelf: true; }, { optional: true; }]>;
364367
}
365368

366369
// @public (undocumented)

0 commit comments

Comments
 (0)