@@ -37,15 +37,18 @@ $.widget( "ui.grid", {
37
37
uiGridBodyTable . find ( "caption" )
38
38
. prependTo ( uiGridHeadTable ) ;
39
39
40
+ // Create COLGROUP and COLs if missing
40
41
if ( ! uiGridBodyTable . find ( "colgroup" ) . length ) {
41
42
// TODO: Consider adding support for existing COL elements not inside COLGROUP
43
+ // TODO: ... in the meantime, remove any that might exist
42
44
uiGridBodyTable . find ( "col" ) . remove ( ) ;
43
45
var colgroup = $ ( "<colgroup></colgroup>" ) . insertBefore ( uiGridBodyTable . find ( "thead" ) ) ;
44
46
uiGridBodyTable . find ( "tr:eq(0)" ) . children ( ) . each ( function ( i ) {
45
47
colgroup . append ( "<col>" ) ;
46
48
} ) ;
47
49
}
48
50
51
+ // Auto-size columns based on relative widths of pre-grid table column widths
49
52
uiGridBody . find ( "colgroup" ) . children ( ) . each ( function ( i ) {
50
53
$ ( this ) . css ( "width" , ( colWidths [ i ] / totalWidth * 100 ) + '%' ) ;
51
54
} ) ;
@@ -55,13 +58,6 @@ $.widget( "ui.grid", {
55
58
. clone ( )
56
59
. appendTo ( uiGridHeadTable ) ;
57
60
58
- // TODO: handle case of no COLs existing
59
- // Set head col widths equal to body col widths
60
- var headCols = uiGridHeadTable . find ( "col" ) ;
61
- uiGridBodyTable . find ( "col" ) . each ( function ( i , a ) {
62
- headCols . eq ( i ) . width ( $ ( this ) . width ( ) ) ;
63
- } ) ;
64
-
65
61
// Move table THEAD to grid head for fixed column headers
66
62
uiGridBodyTable . find ( "thead" )
67
63
. appendTo ( uiGridHeadTable ) ;
@@ -78,6 +74,17 @@ $.widget( "ui.grid", {
78
74
// Give body cells a clickable state
79
75
uiGridBodyTable . find ( "td" ) . addClass ( "ui-state-default" ) ;
80
76
77
+ this . refresh ( ) ;
78
+
79
+ } ,
80
+ refresh : function ( ) {
81
+ // Adjust head in case of visible scrollbar on body to keep columns aligned
82
+ var vertScrollbar = ( this . uiGridBody [ 0 ] . scrollHeight !== this . uiGridBody [ 0 ] . clientHeight ) ;
83
+ if ( vertScrollbar ) {
84
+ this . uiGridHead . css ( "padding-right" , ( this . uiGridBody . width ( ) - this . uiGridBodyTable . width ( ) ) + "px" ) ;
85
+ } else {
86
+ this . uiGridHead . css ( "padding-right" , 0 ) ;
87
+ }
81
88
}
82
89
} ) ;
83
90
0 commit comments