Skip to content

Commit 794724a

Browse files
committed
Fixed column alignments, added refresh method that can be called when scrolling is or isn't present.
1 parent 86c9c3e commit 794724a

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

grid-markup/grid.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Functional
88
.ui-grid .ui-grid-head-table, .ui-grid .ui-grid-body-table {
99
border-collapse: collapse;
1010
table-layout: fixed;
11+
width: 100%;
1112
}
1213
.ui-grid .ui-grid-body-table thead th {
1314
height: 0!important;

grid-markup/grid.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</head>
2626
<body>
2727

28+
<div style="width: 450px;">
2829
<table id="dataTable1">
2930
<thead>
3031
<tr>
@@ -52,10 +53,11 @@
5253
</tr>
5354
</tbody>
5455
</table>
56+
</div>
5557

5658
<p></p>
5759

58-
<table id="dataTable2" style="width:100%;">
60+
<table id="dataTable2">
5961
<caption>People are strange when you're a stranger</caption>
6062
<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup>
6163
<thead>

grid-markup/grid.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ $.widget( "ui.grid", {
3737
uiGridBodyTable.find("caption")
3838
.prependTo( uiGridHeadTable );
3939

40+
// Create COLGROUP and COLs if missing
4041
if ( !uiGridBodyTable.find("colgroup").length ) {
4142
// TODO: Consider adding support for existing COL elements not inside COLGROUP
43+
// TODO: ... in the meantime, remove any that might exist
4244
uiGridBodyTable.find("col").remove();
4345
var colgroup = $("<colgroup></colgroup>").insertBefore( uiGridBodyTable.find("thead") );
4446
uiGridBodyTable.find("tr:eq(0)").children().each(function(i) {
4547
colgroup.append("<col>");
4648
});
4749
}
4850

51+
// Auto-size columns based on relative widths of pre-grid table column widths
4952
uiGridBody.find("colgroup").children().each(function(i) {
5053
$(this).css("width", ( colWidths[i] / totalWidth * 100 ) + '%' );
5154
});
@@ -55,13 +58,6 @@ $.widget( "ui.grid", {
5558
.clone()
5659
.appendTo( uiGridHeadTable );
5760

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-
6561
// Move table THEAD to grid head for fixed column headers
6662
uiGridBodyTable.find("thead")
6763
.appendTo( uiGridHeadTable );
@@ -78,6 +74,17 @@ $.widget( "ui.grid", {
7874
// Give body cells a clickable state
7975
uiGridBodyTable.find("td").addClass("ui-state-default");
8076

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+
}
8188
}
8289
});
8390

0 commit comments

Comments
 (0)