@@ -29,6 +29,7 @@ import {
29
29
} from '@angular/core' ;
30
30
import { DOCUMENT , NgTemplateOutlet , NgIf , NgClass , NgStyle , NgFor } from '@angular/common' ;
31
31
32
+ import { first } from 'rxjs/operators' ;
32
33
import { IgxGridBaseDirective } from '../grid-base.directive' ;
33
34
import { IgxFilteringService } from '../filtering/grid-filtering.service' ;
34
35
import { IgxGridSelectionService } from '../selection/selection.service' ;
@@ -1156,6 +1157,18 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
1156
1157
this . rowDimensionWidthToPixels ( this . emptyRowDimension ) ;
1157
1158
}
1158
1159
1160
+ /**
1161
+ * @hidden @internal
1162
+ */
1163
+ public rowDimensionWidth ( dim , ignoreBeforeInit = false ) : string {
1164
+ const isAuto = dim . width && dim . width . indexOf ( 'auto' ) !== - 1 ;
1165
+ if ( isAuto ) {
1166
+ return dim . autoWidth ? dim . autoWidth + 'px' : 'fit-content' ;
1167
+ } else {
1168
+ return this . rowDimensionWidthToPixels ( dim , ignoreBeforeInit ) + 'px' ;
1169
+ }
1170
+ }
1171
+
1159
1172
/**
1160
1173
* @hidden @internal
1161
1174
*/
@@ -1168,8 +1181,11 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
1168
1181
return MINIMUM_COLUMN_WIDTH ;
1169
1182
}
1170
1183
const isPercent = dim . width && dim . width . indexOf ( '%' ) !== - 1 ;
1184
+ const isAuto = dim . width && dim . width . indexOf ( 'auto' ) !== - 1 ;
1171
1185
if ( isPercent ) {
1172
1186
return parseFloat ( dim . width ) / 100 * this . calcWidth ;
1187
+ } else if ( isAuto ) {
1188
+ return dim . autoWidth ;
1173
1189
} else {
1174
1190
return parseInt ( dim . width , 10 ) ;
1175
1191
}
@@ -2035,6 +2051,41 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
2035
2051
return columns ;
2036
2052
}
2037
2053
2054
+ protected override calculateGridSizes ( recalcFeatureWidth = true ) {
2055
+ super . calculateGridSizes ( recalcFeatureWidth ) ;
2056
+ if ( this . hasDimensionsToAutosize ) {
2057
+ this . cdr . detectChanges ( ) ;
2058
+ this . zone . onStable . pipe ( first ( ) ) . subscribe ( ( ) => {
2059
+ this . autoSizeDimensionsInView ( ) ;
2060
+ } ) ;
2061
+ }
2062
+ }
2063
+
2064
+ protected autoSizeDimensionsInView ( ) {
2065
+ if ( ! this . hasDimensionsToAutosize ) return ;
2066
+ for ( const dim of this . rowDimensions ) {
2067
+ if ( dim . width === 'auto' ) {
2068
+ const contentWidths = [ ] ;
2069
+ const relatedDims = PivotUtil . flatten ( [ dim ] ) . map ( x => x . memberName ) ;
2070
+ const content = this . rowDimensionContentCollection . filter ( x => relatedDims . indexOf ( x . dimension . memberName ) !== - 1 ) ;
2071
+ const headers = content . map ( x => x . headerGroups . toArray ( ) ) . flat ( ) . map ( x => x . header && x . header . refInstance ) ;
2072
+ headers . forEach ( ( header ) => contentWidths . push ( header ?. nativeElement ?. offsetWidth || 0 ) ) ;
2073
+ const max = Math . max ( ...contentWidths ) ;
2074
+ if ( max === 0 ) {
2075
+ // cells not in DOM yet...
2076
+ continue ;
2077
+ }
2078
+ const maxSize = Math . ceil ( Math . max ( ...contentWidths ) ) ;
2079
+ dim . autoWidth = maxSize ;
2080
+ }
2081
+ }
2082
+ }
2083
+
2084
+ /** @hidden @internal */
2085
+ public get hasDimensionsToAutosize ( ) {
2086
+ return this . rowDimensions . some ( x => x . width === 'auto' && ! x . autoWidth ) ;
2087
+ }
2088
+
2038
2089
protected generateFromData ( fields : string [ ] ) {
2039
2090
const separator = this . pivotKeys . columnDimensionSeparator ;
2040
2091
const dataArr = fields . map ( x => x . split ( separator ) ) . sort ( x => x . length ) ;
@@ -2158,13 +2209,14 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
2158
2209
const count = this . values . length ;
2159
2210
const childWidth = parseInt ( parentWidth , 10 ) / count ;
2160
2211
const isPercent = parentWidth && parentWidth . indexOf ( '%' ) !== - 1 ;
2212
+ const isAuto = parentWidth && parentWidth . indexOf ( 'auto' ) !== - 1 ;
2161
2213
this . values . forEach ( val => {
2162
2214
const ref = createComponent ( IgxColumnComponent , { environmentInjector : this . envInjector , elementInjector : this . injector } ) ;
2163
2215
ref . instance . header = val . displayName || val . member ;
2164
2216
ref . instance . field = parent . field + this . pivotKeys . columnDimensionSeparator + val . member ;
2165
2217
ref . instance . parent = parent ;
2166
2218
if ( parentWidth ) {
2167
- ref . instance . width = isPercent ? childWidth + '%' : childWidth + 'px' ;
2219
+ ref . instance . width = isAuto ? 'auto' : isPercent ? childWidth + '%' : childWidth + 'px' ;
2168
2220
}
2169
2221
ref . instance . hidden = hidden ;
2170
2222
ref . instance . sortable = true ;
0 commit comments