@@ -23,7 +23,8 @@ import {
23
23
ViewContainerRef ,
24
24
Injector ,
25
25
NgModuleRef ,
26
- ApplicationRef
26
+ ApplicationRef ,
27
+ ContentChildren
27
28
} from '@angular/core' ;
28
29
import { IgxGridBaseDirective } from '../grid-base.directive' ;
29
30
import { IgxFilteringService } from '../filtering/grid-filtering.service' ;
@@ -62,6 +63,8 @@ import { IgxGridTransaction } from '../common/types';
62
63
import { SortingDirection } from '../../data-operations/sorting-strategy' ;
63
64
import { GridBaseAPIService } from '../api.service' ;
64
65
import { IgxGridForOfDirective } from '../../directives/for-of/for_of.directive' ;
66
+ import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-content.component' ;
67
+ import { flatten } from '@angular/compiler' ;
65
68
66
69
let NEXT_ID = 0 ;
67
70
const MINIMUM_COLUMN_WIDTH = 200 ;
@@ -993,6 +996,75 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
993
996
super . setupColumns ( ) ;
994
997
}
995
998
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
+
996
1068
protected resolveToggle ( groupColumn : IgxColumnComponent , state : boolean ) {
997
1069
groupColumn . hidden = state ;
998
1070
this . columnGroupStates . set ( groupColumn . field , state ) ;
@@ -1042,12 +1114,12 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
1042
1114
/**
1043
1115
* @hidden @internal
1044
1116
*/
1045
- @ViewChildren ( 'verticalRowDimScrollContainer' , { read : IgxGridForOfDirective } )
1117
+ @ViewChildren ( 'verticalRowDimScrollContainer' , { read : IgxGridForOfDirective } )
1046
1118
public verticalRowDimScrollContainers : QueryList < IgxGridForOfDirective < any > > ;
1047
1119
1048
1120
protected verticalScrollHandler ( event ) {
1049
1121
super . verticalScrollHandler ( event ) ;
1050
- this . verticalRowDimScrollContainers . forEach ( x => {
1122
+ this . verticalRowDimScrollContainers . forEach ( x => {
1051
1123
x . onScroll ( event ) ;
1052
1124
x . cdr . detectChanges ( ) ;
1053
1125
} ) ;
0 commit comments