@@ -609,20 +609,23 @@ angular.module('ui.grid')
609609 asteriskNum = 0 ,
610610 usedWidthSum = 0 ,
611611 ret = '' ,
612- pinRightColumn = false ;
612+ pinRightColumn = false ,
613+ fixedNumberArray = [ ] ,
614+ percentageArray = [ ] ,
615+ totalPercentage = 0 ;
613616
614617 // Get the width of the viewport
615618 var availableWidth = self . grid . getViewportWidth ( ) - self . grid . scrollbarWidth ;
616619
617620 // get all the columns across all render containers, we have to calculate them all or one render container
618621 // could consume the whole viewport
619622 var columnCache = [ ] ;
620- angular . forEach ( self . grid . renderContainers , function ( container , name ) {
623+ angular . forEach ( self . grid . renderContainers , function ( container , name ) {
621624 columnCache = columnCache . concat ( container . visibleColumnCache ) ;
622625 } ) ;
623626
624627 // look at each column, process any manual values or %, put the * into an array to look at later
625- columnCache . forEach ( function ( column , i ) {
628+ columnCache . forEach ( function ( column , i ) {
626629 var width = 0 ;
627630 // Skip hidden columns
628631 if ( ! column . visible ) { return ; }
@@ -641,21 +644,26 @@ angular.module('ui.grid')
641644 usedWidthSum = usedWidthSum + width ;
642645 column . drawnWidth = width ;
643646
647+ fixedNumberArray . push ( column ) ;
644648 } else if ( gridUtil . endsWith ( column . width , "%" ) ) {
645649 // percentage width, set to percentage of the viewport
646650 // round down to int - some browsers don't play nice with float maxWidth
647- width = parseInt ( parseInt ( column . width . replace ( / % / g, '' ) , 10 ) / 100 * availableWidth ) ;
651+ var percentageIntegerValue = parseInt ( column . width . replace ( / % / g, '' ) , 10 ) ;
652+ width = parseInt ( percentageIntegerValue / 100 * availableWidth ) ;
648653
649- if ( width > column . maxWidth ) {
654+ if ( width > column . maxWidth ) {
650655 width = column . maxWidth ;
651656 }
652657
653- if ( width < column . minWidth ) {
658+ if ( width < column . minWidth ) {
654659 width = column . minWidth ;
655660 }
656661
657662 usedWidthSum = usedWidthSum + width ;
658663 column . drawnWidth = width ;
664+
665+ totalPercentage = totalPercentage + percentageIntegerValue ;
666+ percentageArray . push ( column ) ;
659667 } else if ( angular . isString ( column . width ) && column . width . indexOf ( '*' ) !== - 1 ) {
660668 // is an asterisk column, the gridColumn already checked the string consists only of '****'
661669 asteriskNum = asteriskNum + column . width . length ;
@@ -672,62 +680,73 @@ angular.module('ui.grid')
672680 // the width that each asterisk value would be assigned (this can be negative)
673681 var asteriskVal = remainingWidth / asteriskNum ;
674682
675- asterisksArray . forEach ( function ( column ) {
683+ asterisksArray . forEach ( function ( column ) {
676684 var width = parseInt ( column . width . length * asteriskVal , 10 ) ;
677685
678- if ( width > column . maxWidth ) {
679- width = column . maxWidth ;
686+ if ( width > column . maxWidth ) {
687+ width = column . maxWidth ;
680688 }
681689
682- if ( width < column . minWidth ) {
683- width = column . minWidth ;
690+ if ( width < column . minWidth ) {
691+ width = column . minWidth ;
684692 }
685693
686694 usedWidthSum = usedWidthSum + width ;
687695 column . drawnWidth = width ;
688696 } ) ;
689697 }
690698
691- // If the grid width didn't divide evenly into the column widths and we have pixels left over, or our
692- // calculated widths would have the grid narrower than the available space,
693- // dole the remainder out one by one to make everything fit
694- var processColumnUpwards = function ( column ) {
695- if ( column . drawnWidth < column . maxWidth && leftoverWidth > 0 ) {
696- column . drawnWidth ++ ;
697- usedWidthSum ++ ;
698- leftoverWidth -- ;
699- columnsToChange = true ;
700- }
701- } ;
699+ // If there are no columns with asterisk widths then check if there are any with % widths and
700+ // use them as a fallback for adjusting column widths up or down if we have remaining grid width
701+ // or need to claw some width back
702+ var variableWidthColumnArray ;
703+ if ( asterisksArray . length > 0 ) {
704+ variableWidthColumnArray = asterisksArray ;
705+ } else if ( percentageArray . length > 0 && fixedNumberArray . length === 0 && totalPercentage === 100 ) {
706+ variableWidthColumnArray = percentageArray ;
707+ }
702708
703- var leftoverWidth = availableWidth - usedWidthSum ;
704- var columnsToChange = true ;
709+ if ( ! angular . isUndefined ( variableWidthColumnArray ) ) {
710+ // If the grid width didn't divide evenly into the column widths and we have pixels left over, or our
711+ // calculated widths would have the grid narrower than the available space,
712+ // dole the remainder out one by one to make everything fit
713+ var processColumnUpwards = function ( column ) {
714+ if ( column . drawnWidth < column . maxWidth && leftoverWidth > 0 ) {
715+ column . drawnWidth ++ ;
716+ usedWidthSum ++ ;
717+ leftoverWidth -- ;
718+ columnsToChange = true ;
719+ }
720+ } ;
705721
706- while ( leftoverWidth > 0 && columnsToChange ) {
707- columnsToChange = false ;
708- asterisksArray . forEach ( processColumnUpwards ) ;
709- }
722+ var leftoverWidth = availableWidth - usedWidthSum ;
723+ var columnsToChange = true ;
710724
711- // We can end up with too much width even though some columns aren't at their max width, in this situation
712- // we can trim the columns a little
713- var processColumnDownwards = function ( column ) {
714- if ( column . drawnWidth > column . minWidth && excessWidth > 0 ) {
715- column . drawnWidth -- ;
716- usedWidthSum -- ;
717- excessWidth -- ;
718- columnsToChange = true ;
725+ while ( leftoverWidth > 0 && columnsToChange ) {
726+ columnsToChange = false ;
727+ variableWidthColumnArray . forEach ( processColumnUpwards ) ;
719728 }
720- } ;
721729
722- var excessWidth = usedWidthSum - availableWidth ;
723- columnsToChange = true ;
730+ // We can end up with too much width even though some columns aren't at their max width, in this situation
731+ // we can trim the columns a little
732+ var processColumnDownwards = function ( column ) {
733+ if ( column . drawnWidth > column . minWidth && excessWidth > 0 ) {
734+ column . drawnWidth -- ;
735+ usedWidthSum -- ;
736+ excessWidth -- ;
737+ columnsToChange = true ;
738+ }
739+ } ;
740+
741+ var excessWidth = usedWidthSum - availableWidth ;
742+ columnsToChange = true ;
724743
725- while ( excessWidth > 0 && columnsToChange ) {
726- columnsToChange = false ;
727- asterisksArray . forEach ( processColumnDownwards ) ;
744+ while ( excessWidth > 0 && columnsToChange ) {
745+ columnsToChange = false ;
746+ variableWidthColumnArray . forEach ( processColumnDownwards ) ;
747+ }
728748 }
729749
730-
731750 // all that was across all the renderContainers, now we need to work out what that calculation decided for
732751 // our renderContainer
733752 var canvasWidth = 0 ;
0 commit comments