@@ -35,7 +35,8 @@ import {
3535 TrackByFunction ,
3636 ViewChild ,
3737 ViewContainerRef ,
38- ViewEncapsulation
38+ ViewEncapsulation ,
39+ ContentChild
3940} from '@angular/core' ;
4041import {
4142 BehaviorSubject ,
@@ -54,7 +55,8 @@ import {
5455 CdkCellOutletRowContext ,
5556 CdkFooterRowDef ,
5657 CdkHeaderRowDef ,
57- CdkRowDef
58+ CdkRowDef ,
59+ CdkNoDataRow
5860} from './row' ;
5961import { StickyStyler } from './sticky-styler' ;
6062import {
@@ -106,6 +108,16 @@ export class FooterRowOutlet implements RowOutlet {
106108 constructor ( public viewContainer : ViewContainerRef , public elementRef : ElementRef ) { }
107109}
108110
111+ /**
112+ * Provides a handle for the table to grab the view
113+ * container's ng-container to insert the no data row.
114+ * @docs -private
115+ */
116+ @Directive ( { selector : '[noDataRowOutlet]' } )
117+ export class NoDataRowOutlet implements RowOutlet {
118+ constructor ( public viewContainer : ViewContainerRef , public elementRef : ElementRef ) { }
119+ }
120+
109121/**
110122 * The table template that can be used by the mat-table. Should not be used outside of the
111123 * material library.
@@ -119,6 +131,7 @@ export const CDK_TABLE_TEMPLATE =
119131 <ng-content select="colgroup, col"></ng-content>
120132 <ng-container headerRowOutlet></ng-container>
121133 <ng-container rowOutlet></ng-container>
134+ <ng-container noDataRowOutlet></ng-container>
122135 <ng-container footerRowOutlet></ng-container>
123136` ;
124137
@@ -293,6 +306,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
293306 */
294307 protected stickyCssClass : string = 'cdk-table-sticky' ;
295308
309+ /** Whether the no data row is currently showing anything. */
310+ private _isShowingNoDataRow = false ;
311+
296312 /**
297313 * Tracking function that will be used to check the differences in data changes. Used similarly
298314 * to `ngFor` `trackBy` function. Optimize row operations by identifying a row based on its data
@@ -379,6 +395,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
379395 @ViewChild ( DataRowOutlet , { static : true } ) _rowOutlet : DataRowOutlet ;
380396 @ViewChild ( HeaderRowOutlet , { static : true } ) _headerRowOutlet : HeaderRowOutlet ;
381397 @ViewChild ( FooterRowOutlet , { static : true } ) _footerRowOutlet : FooterRowOutlet ;
398+ @ViewChild ( NoDataRowOutlet , { static : true } ) _noDataRowOutlet : NoDataRowOutlet ;
382399
383400 /**
384401 * The column definitions provided by the user that contain what the header, data, and footer
@@ -399,6 +416,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
399416 descendants : true
400417 } ) _contentFooterRowDefs : QueryList < CdkFooterRowDef > ;
401418
419+ /** Row definition that will only be rendered if there's no data in the table. */
420+ @ContentChild ( CdkNoDataRow ) _noDataRow : CdkNoDataRow ;
421+
402422 constructor (
403423 protected readonly _differs : IterableDiffers ,
404424 protected readonly _changeDetectorRef : ChangeDetectorRef ,
@@ -464,6 +484,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
464484
465485 ngOnDestroy ( ) {
466486 this . _rowOutlet . viewContainer . clear ( ) ;
487+ this . _noDataRowOutlet . viewContainer . clear ( ) ;
467488 this . _headerRowOutlet . viewContainer . clear ( ) ;
468489 this . _footerRowOutlet . viewContainer . clear ( ) ;
469490
@@ -519,6 +540,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
519540 rowView . context . $implicit = record . item . data ;
520541 } ) ;
521542
543+ this . _updateNoDataRow ( ) ;
522544 this . updateStickyColumnStyles ( ) ;
523545 }
524546
@@ -1017,15 +1039,19 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
10171039 private _applyNativeTableSections ( ) {
10181040 const documentFragment = this . _document . createDocumentFragment ( ) ;
10191041 const sections = [
1020- { tag : 'thead' , outlet : this . _headerRowOutlet } ,
1021- { tag : 'tbody' , outlet : this . _rowOutlet } ,
1022- { tag : 'tfoot' , outlet : this . _footerRowOutlet } ,
1042+ { tag : 'thead' , outlets : [ this . _headerRowOutlet ] } ,
1043+ { tag : 'tbody' , outlets : [ this . _rowOutlet , this . _noDataRowOutlet ] } ,
1044+ { tag : 'tfoot' , outlets : [ this . _footerRowOutlet ] } ,
10231045 ] ;
10241046
10251047 for ( const section of sections ) {
10261048 const element = this . _document . createElement ( section . tag ) ;
10271049 element . setAttribute ( 'role' , 'rowgroup' ) ;
1028- element . appendChild ( section . outlet . elementRef . nativeElement ) ;
1050+
1051+ for ( const outlet of section . outlets ) {
1052+ element . appendChild ( outlet . elementRef . nativeElement ) ;
1053+ }
1054+
10291055 documentFragment . appendChild ( element ) ;
10301056 }
10311057
@@ -1094,6 +1120,19 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
10941120 return items . filter ( item => ! item . _table || item . _table === this ) ;
10951121 }
10961122
1123+ /** Creates or removes the no data row, depending on whether any data is being shown. */
1124+ private _updateNoDataRow ( ) {
1125+ if ( this . _noDataRow ) {
1126+ const shouldShow = this . _rowOutlet . viewContainer . length === 0 ;
1127+
1128+ if ( shouldShow !== this . _isShowingNoDataRow ) {
1129+ const container = this . _noDataRowOutlet . viewContainer ;
1130+ shouldShow ? container . createEmbeddedView ( this . _noDataRow . templateRef ) : container . clear ( ) ;
1131+ this . _isShowingNoDataRow = shouldShow ;
1132+ }
1133+ }
1134+ }
1135+
10971136 static ngAcceptInputType_multiTemplateDataRows : BooleanInput ;
10981137}
10991138
0 commit comments