@@ -23,7 +23,8 @@ import {
2323 ViewContainerRef ,
2424 Injector ,
2525 NgModuleRef ,
26- ApplicationRef
26+ ApplicationRef ,
27+ ContentChildren
2728} from '@angular/core' ;
2829import { IgxGridBaseDirective } from '../grid-base.directive' ;
2930import { IgxFilteringService } from '../filtering/grid-filtering.service' ;
@@ -62,6 +63,8 @@ import { IgxGridTransaction } from '../common/types';
6263import { SortingDirection } from '../../data-operations/sorting-strategy' ;
6364import { GridBaseAPIService } from '../api.service' ;
6465import { IgxGridForOfDirective } from '../../directives/for-of/for_of.directive' ;
66+ import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-content.component' ;
67+ import { flatten } from '@angular/compiler' ;
6568
6669let NEXT_ID = 0 ;
6770const MINIMUM_COLUMN_WIDTH = 200 ;
@@ -993,6 +996,75 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
993996 super . setupColumns ( ) ;
994997 }
995998
999+ /**
1000+ * Auto-sizes row dimension cells.
1001+ *
1002+ * @remarks
1003+ * Only sizes based on the dimension cells in view.
1004+ * @example
1005+ * ```typescript
1006+ * this.grid.autoSizeRowDimension(dimension);
1007+ * ```
1008+ * @param dimension The row dimension to size.
1009+ */
1010+ public autoSizeRowDimension ( dimension : IPivotDimension ) {
1011+ if ( this . getDimensionType ( dimension ) === PivotDimensionType . Row ) {
1012+ const relatedDims = PivotUtil . flatten ( [ dimension ] ) . map ( x => x . memberName ) ;
1013+ const content = this . rowDimensionContentCollection . filter ( x => relatedDims . indexOf ( x . dimension . memberName ) !== - 1 ) ;
1014+ const headers = flatten ( content . map ( x => x . headerGroups . toArray ( ) ) ) . map ( x => x . header . refInstance ) ;
1015+ const autoWidth = this . getLargesContentWidth ( headers ) ;
1016+ dimension . width = autoWidth ;
1017+ this . pipeTrigger ++ ;
1018+ }
1019+ }
1020+
1021+ @ViewChildren ( IgxPivotRowDimensionContentComponent )
1022+ protected rowDimensionContentCollection : QueryList < IgxPivotRowDimensionContentComponent > ;
1023+
1024+ protected getElementContentWidth ( element : HTMLElement ) : any {
1025+ const range = this . document . createRange ( ) ;
1026+ const headerWidth = this . platform . getNodeSizeViaRange ( range ,
1027+ element ,
1028+ element . parentElement ) ;
1029+
1030+ const headerStyle = this . document . defaultView . getComputedStyle ( element ) ;
1031+ const headerPadding = parseFloat ( headerStyle . paddingLeft ) + parseFloat ( headerStyle . paddingRight ) +
1032+ parseFloat ( headerStyle . borderRightWidth ) ;
1033+
1034+ return { width : Math . ceil ( headerWidth ) , padding : Math . ceil ( headerPadding ) } ;
1035+ }
1036+
1037+ protected getDimensionType ( dimension : IPivotDimension ) : PivotDimensionType {
1038+ return PivotUtil . flatten ( this . rowDimensions ) . indexOf ( dimension ) !== - 1 ? PivotDimensionType . Row :
1039+ PivotUtil . flatten ( this . columnDimensions ) . indexOf ( dimension ) !== - 1 ? PivotDimensionType . Column : PivotDimensionType . Filter ;
1040+ }
1041+
1042+ protected getLargesContentWidth ( contents : ElementRef [ ] ) : string {
1043+ const range = this . document . createRange ( ) ;
1044+ const largest = new Map < number , number > ( ) ;
1045+
1046+ if ( contents . length > 0 ) {
1047+ const cellsContentWidths = [ ] ;
1048+ contents . forEach ( ( elem ) => cellsContentWidths . push ( this . getElementContentWidth ( elem . nativeElement ) . width ) ) ;
1049+
1050+ const index = cellsContentWidths . indexOf ( Math . max ( ...cellsContentWidths ) ) ;
1051+ const cellStyle = this . document . defaultView . getComputedStyle ( contents [ index ] . nativeElement ) ;
1052+ const cellPadding = parseFloat ( cellStyle . paddingLeft ) + parseFloat ( cellStyle . paddingRight ) +
1053+ parseFloat ( cellStyle . borderLeftWidth ) + parseFloat ( cellStyle . borderRightWidth ) ;
1054+
1055+ largest . set ( Math . max ( ...cellsContentWidths ) , cellPadding ) ;
1056+ }
1057+
1058+ const largestCell = Math . max ( ...Array . from ( largest . keys ( ) ) ) ;
1059+ const width = Math . ceil ( largestCell + largest . get ( largestCell ) ) ;
1060+
1061+ if ( Number . isNaN ( width ) ) {
1062+ return null ;
1063+ } else {
1064+ return width + 'px' ;
1065+ }
1066+ }
1067+
9961068 protected resolveToggle ( groupColumn : IgxColumnComponent , state : boolean ) {
9971069 groupColumn . hidden = state ;
9981070 this . columnGroupStates . set ( groupColumn . field , state ) ;
@@ -1042,12 +1114,12 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10421114 /**
10431115 * @hidden @internal
10441116 */
1045- @ViewChildren ( 'verticalRowDimScrollContainer' , { read : IgxGridForOfDirective } )
1117+ @ViewChildren ( 'verticalRowDimScrollContainer' , { read : IgxGridForOfDirective } )
10461118 public verticalRowDimScrollContainers : QueryList < IgxGridForOfDirective < any > > ;
10471119
10481120 protected verticalScrollHandler ( event ) {
10491121 super . verticalScrollHandler ( event ) ;
1050- this . verticalRowDimScrollContainers . forEach ( x => {
1122+ this . verticalRowDimScrollContainers . forEach ( x => {
10511123 x . onScroll ( event ) ;
10521124 x . cdr . detectChanges ( ) ;
10531125 } ) ;
0 commit comments