@@ -2071,6 +2071,35 @@ describe('CdkTable', () => {
20712071 await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
20722072 }
20732073
2074+ it ( 'should render ordinary tables inside virtual scroll items without attaching them to the viewport' , async ( ) => {
2075+ const { fixture} = await createVirtualScroll ( TablesInsideVirtualScrollItems ) ;
2076+ const viewport = fixture . componentInstance . viewport ;
2077+ const checkRenderedTables = ( ) => {
2078+ const groups : HTMLElement [ ] = Array . from (
2079+ fixture . nativeElement . querySelectorAll ( '.table-group' ) ,
2080+ ) ;
2081+
2082+ expect ( groups . length ) . toBeGreaterThan ( 0 ) ;
2083+ expect ( groups . length ) . toBeLessThan ( fixture . componentInstance . groups . length ) ;
2084+ expect ( viewport . getDataLength ( ) ) . toBe ( fixture . componentInstance . groups . length ) ;
2085+
2086+ for ( const group of groups ) {
2087+ const table = group . querySelector ( 'table' ) ! ;
2088+ const data = fixture . componentInstance . groups [ Number ( group . getAttribute ( 'data-group' ) ) ] ;
2089+ expect ( getRows ( table ) . map ( row => row . textContent ! . trim ( ) ) ) . toEqual ( data . rows ) ;
2090+ expect ( table . classList ) . not . toContain ( 'cdk-table-fixed-layout' ) ;
2091+ }
2092+ } ;
2093+
2094+ checkRenderedTables ( ) ;
2095+ await triggerScroll ( fixture , 1000 ) ;
2096+ await fixture . whenStable ( ) ;
2097+ fixture . detectChanges ( ) ;
2098+
2099+ expect ( viewport . getRenderedRange ( ) . start ) . toBeGreaterThan ( 0 ) ;
2100+ checkRenderedTables ( ) ;
2101+ } ) ;
2102+
20742103 it ( 'should not render the full data set when using virtual scrolling' , async ( ) => {
20752104 const { fixture, table} = await createVirtualScroll ( TableWithVirtualScroll ) ;
20762105 expect ( fixture . componentInstance . dataSource . data . length ) . toBeGreaterThan ( 2000 ) ;
@@ -3471,6 +3500,34 @@ class TableWithVirtualScrollAndMultipleDefinitions extends TableWithVirtualScrol
34713500 predicate = ( ) => true ;
34723501}
34733502
3503+ @Component ( {
3504+ template : `
3505+ <cdk-virtual-scroll-viewport itemSize="100" style="height: 200px; width: 400px">
3506+ <div
3507+ *cdkVirtualFor="let group of groups"
3508+ class="table-group"
3509+ style="height: 100px"
3510+ [attr.data-group]="group.id">
3511+ <table cdk-table [dataSource]="group.rows">
3512+ <ng-container cdkColumnDef="value">
3513+ <td cdk-cell *cdkCellDef="let row">{{row}}</td>
3514+ </ng-container>
3515+ <tr cdk-row *cdkRowDef="let row; columns: ['value']"></tr>
3516+ </table>
3517+ </div>
3518+ </cdk-virtual-scroll-viewport>
3519+ ` ,
3520+ imports : [ CdkTableModule , ScrollingModule ] ,
3521+ changeDetection : ChangeDetectionStrategy . Eager ,
3522+ } )
3523+ class TablesInsideVirtualScrollItems {
3524+ @ViewChild ( CdkVirtualScrollViewport ) viewport ! : CdkVirtualScrollViewport ;
3525+ groups = Array . from ( { length : 100 } , ( _ , id ) => ( {
3526+ id,
3527+ rows : Array . from ( { length : 3 } , ( _ , row ) => `group ${ id } , row ${ row } ` ) ,
3528+ } ) ) ;
3529+ }
3530+
34743531function getElements ( element : Element , query : string ) : HTMLElement [ ] {
34753532 return [ ] . slice . call ( element . querySelectorAll ( query ) ) ;
34763533}
0 commit comments