@@ -20,11 +20,7 @@ import {
2020 ListRange ,
2121} from '../collections' ;
2222import { Platform } from '../platform' ;
23- import {
24- CDK_VIRTUAL_SCROLL_VIEWPORT ,
25- type CdkVirtualScrollViewport ,
26- ViewportRuler ,
27- } from '../scrolling' ;
23+ import { type CdkVirtualScrollViewport , ViewportRuler } from '../scrolling' ;
2824
2925import {
3026 AfterContentChecked ,
@@ -301,7 +297,6 @@ export class CdkTable<T>
301297 protected _viewRepeater : _ViewRepeater < T , RenderRow < T > , RowContext < T > > ;
302298 private readonly _viewportRuler = inject ( ViewportRuler ) ;
303299 private _injector = inject ( Injector ) ;
304- private _virtualScrollViewport = inject ( CDK_VIRTUAL_SCROLL_VIEWPORT , { optional : true } ) ;
305300 private _positionListener =
306301 inject ( STICKY_POSITIONING_LISTENER , { optional : true } ) ||
307302 inject ( STICKY_POSITIONING_LISTENER , { optional : true , skipSelf : true } ) ;
@@ -565,7 +560,7 @@ export class CdkTable<T>
565560 get fixedLayout ( ) : boolean {
566561 // Require a fixed layout when virtual scrolling is enabled, otherwise
567562 // the element the header can jump around as the user is scrolling.
568- return this . _virtualScrollViewport ? true : this . _fixedLayout ;
563+ return this . virtualScroll ? true : this . _fixedLayout ;
569564 }
570565 set fixedLayout ( value : boolean ) {
571566 this . _fixedLayout = value ;
@@ -582,6 +577,9 @@ export class CdkTable<T>
582577 */
583578 @Input ( { transform : booleanAttribute } ) recycleRows = false ;
584579
580+ /** Viewport used to enable virtual scrolling on the table. */
581+ @Input ( ) virtualScroll : CdkVirtualScrollViewport | null = null ;
582+
585583 /**
586584 * Emits when the table completes rendering a set of data rows based on the latest data from the
587585 * data source, even if the set of rows is empty.
@@ -595,7 +593,10 @@ export class CdkTable<T>
595593 *
596594 * @docs -private
597595 */
598- readonly viewChange : BehaviorSubject < ListRange > ;
596+ readonly viewChange : BehaviorSubject < ListRange > = new BehaviorSubject < ListRange > ( {
597+ start : 0 ,
598+ end : Number . MAX_VALUE ,
599+ } ) ;
599600
600601 // Outlets in the table's template where the header, data rows, and footer will be inserted.
601602 _rowOutlet : DataRowOutlet ;
@@ -638,21 +639,13 @@ export class CdkTable<T>
638639
639640 this . _isServer = ! this . _platform . isBrowser ;
640641 this . _isNativeHtmlTable = this . _elementRef . nativeElement . nodeName === 'TABLE' ;
641- this . viewChange = new BehaviorSubject < ListRange > ( {
642- start : 0 ,
643- end : this . _virtualScrollViewport ? 0 : Number . MAX_VALUE ,
644- } ) ;
645642
646643 // Set up the trackBy function so that it uses the `RenderRow` as its identity by default. If
647644 // the user has provided a custom trackBy, return the result of that function as evaluated
648645 // with the values of the `RenderRow`'s data and index.
649646 this . _dataDiffer = this . _differs . find ( [ ] ) . create ( ( _i : number , dataRow : RenderRow < T > ) => {
650647 return this . trackBy ? this . trackBy ( dataRow . dataIndex , dataRow . data ) : dataRow ;
651648 } ) ;
652-
653- if ( this . _virtualScrollViewport ) {
654- this . _setupVirtualScrolling ( this . _virtualScrollViewport ) ;
655- }
656649 }
657650
658651 ngOnInit ( ) {
@@ -668,9 +661,14 @@ export class CdkTable<T>
668661
669662 ngAfterContentInit ( ) {
670663 this . _viewRepeater =
671- this . recycleRows || this . _virtualScrollViewport
664+ this . recycleRows || this . virtualScroll
672665 ? new _RecycleViewRepeaterStrategy ( )
673666 : new _DisposeViewRepeaterStrategy ( ) ;
667+
668+ if ( this . virtualScroll ) {
669+ this . _setupVirtualScrolling ( this . virtualScroll ) ;
670+ }
671+
674672 this . _hasInitialized = true ;
675673 }
676674
@@ -1480,6 +1478,8 @@ export class CdkTable<T>
14801478 const virtualScrollScheduler =
14811479 typeof requestAnimationFrame !== 'undefined' ? animationFrameScheduler : asapScheduler ;
14821480
1481+ this . viewChange . next ( { start : 0 , end : 0 } ) ;
1482+
14831483 // Forward the rendered range computed by the virtual scroll viewport to the table.
14841484 viewport . renderedRangeStream
14851485 // We need the scheduler here, because the virtual scrolling module uses an identical
0 commit comments