1- import * as get from 'lodash/object/get' ;
1+
22import Measurer from './Measurer' ;
33import UiGridMetrics from './UiGridMetrics' ;
44
5- interface IExtendedColumnDef extends uiGrid . IColumnDef {
5+ export interface IExtendedColumnDef extends uiGrid . IColumnDef {
66 enableColumnAutoFit : boolean ;
77}
88
9- interface IExtendedGridColumn extends uiGrid . IGridColumn {
9+ export interface IExtendedGridColumn extends uiGrid . IGridColumn {
1010 colDef : IExtendedColumnDef ;
11+ hasCustomWidth : boolean ;
12+ minWidth : number ;
13+ maxWidth : number ;
14+ grid : IExtendedGridInstance ;
1115}
1216
13- interface IExtendedGridInstance extends uiGrid . IGridInstance {
17+ export interface IExtendedGridInstance extends uiGrid . IGridInstance {
1418 options : IExtendedGridOptions ;
19+ id : number ;
1520}
1621
17- interface IExtendedGridOptions extends uiGrid . IGridOptions {
22+ export interface IExtendedGridOptions extends uiGrid . IGridOptions {
1823 enableColumnAutoFit : boolean ;
1924}
2025
21- interface IAnyFilterPredicateFunc {
26+ export interface IAnyFilterPredicateFunc {
2227 ( value : any , firstFlag ?: any , secondFlag ?: any ) : string ;
2328}
2429
@@ -87,33 +92,40 @@ export class UiGridAutoFitColumnsService {
8792 }
8893
8994 columnsProcessor ( renderedColumnsToProcess ?: Array < IExtendedGridColumn > , rows ?: Array < uiGrid . IGridRow > ) {
95+
9096 if ( ! rows . length ) {
9197 return renderedColumnsToProcess ;
9298 }
93- // TODO: respect existing colDef options
94- // if (col.colDef.enableColumnAutoFitting === false) return;
9599
96100 let optimalWidths : {
97101 [ name : string ] : number
98102 } = { } ;
99103
104+ /* to be able to calculate the width of a column in any browser, we need to temporarily deactivate the min-width and max-width attributes set before */
105+ let tempCSS = '' ;
106+ renderedColumnsToProcess . forEach ( function ( column ) {
107+ //30px and 9000px are the default values for minWidth and maxWidth in UI Grid.
108+ tempCSS += ' .grid' + column . grid . id + ' ' + column . getColClass ( true ) + ' { min-width: 30px !important; max-width: 9000px !important; }' ;
109+ } ) ;
110+ let tempStyle = $ ( '<style>' ) . text ( tempCSS ) . appendTo ( 'body' ) ;
100111
101112 renderedColumnsToProcess . forEach ( column => {
102113
103- if ( column . colDef . enableColumnAutoFit ) {
114+ if ( column . colDef . enableColumnAutoFit && ! column . hasCustomWidth ) {
104115 const columnKey = column . field || column . name ;
105- optimalWidths [ columnKey ] = Measurer . measureRoundedTextWidth ( column . displayName , this . gridMetrics . getHeaderFont ( ) ) + this . gridMetrics . getHeaderButtonsWidth ( ) ;
106-
107- rows . forEach ( ( row ) => {
108- let cellText = get ( row . entity , columnKey ) ;
109-
110- if ( ! ! column . colDef . cellFilter ) {
111- cellText = this . getFilteredValue ( cellText , column . colDef . cellFilter ) ;
112- }
113-
114- const currentCellWidth = Measurer . measureRoundedTextWidth ( cellText , this . gridMetrics . getCellFont ( ) ) ;
115- const optimalCellWidth = currentCellWidth > 300 ? 300 : currentCellWidth ;
116-
116+
117+ let currentHeaderWidth = 0 ;
118+ if ( $ ( '.ui-grid-header-cell' + column . getColClass ( true ) ) [ 0 ] !== undefined ) {
119+ currentHeaderWidth = $ ( '.ui-grid-header-cell' + column . getColClass ( true ) ) [ 0 ] . scrollWidth ;
120+ }
121+ let optimalHeaderWidth = currentHeaderWidth < column . minWidth ? column . minWidth : currentHeaderWidth ;
122+ optimalHeaderWidth = optimalHeaderWidth > column . maxWidth ? column . maxWidth : optimalHeaderWidth ;
123+ optimalWidths [ columnKey ] = optimalHeaderWidth ;
124+
125+ $ ( column . getColClass ( true ) ) . each ( function ( ) {
126+ let currentCellWidth = this . scrollWidth ;
127+ let optimalCellWidth = currentCellWidth < column . minWidth ? column . minWidth : currentCellWidth ;
128+ optimalCellWidth = optimalCellWidth > column . maxWidth ? column . maxWidth : optimalCellWidth ;
117129 if ( optimalCellWidth > optimalWidths [ columnKey ] ) {
118130 optimalWidths [ columnKey ] = optimalCellWidth ;
119131 }
@@ -123,6 +135,9 @@ export class UiGridAutoFitColumnsService {
123135 column . updateColumnDef ( column . colDef , false ) ;
124136 }
125137 } ) ;
138+
139+ tempStyle . remove ( ) ;
140+
126141 return renderedColumnsToProcess ;
127142 }
128143
0 commit comments