Skip to content

Commit 64941b3

Browse files
committed
fix(uiGridHeader): handle leftover pixels
Grid widths that can't be equally divided among columns (i.e. 500px / 3 columns == 166px with 2px leftover) was leaving the 2px visible at the end of the grid. This fix divides the extra pixels among the columns starting at the front.
1 parent 2b55324 commit 64941b3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/features/resize-columns/test/resizeColumns.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ describe('ui.grid.resizeColumns', function () {
9494
});
9595

9696
// NOTE: these pixel sizes might fail in other browsers, due to font differences!
97-
describe('when double-clicking a resizer', function () {
97+
describe('double-clicking a resizer', function () {
9898
it('should resize the column to the maximum width of the rendered columns', function (done) {
9999
var firstResizer = $(grid).find('[ui-grid-column-resizer]').first();
100100

101101
var colWidth = $(grid).find('.col0').first().width();
102102

103-
expect(colWidth).toEqual(166);
103+
expect(colWidth === 166 || colWidth === 167).toBe(true); // allow for column widths that don't equally divide
104104

105105
firstResizer.trigger('dblclick');
106106

src/js/core/directives/ui-grid-header.js

+29-7
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
column.drawnWidth = column.width;
9595

96-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.width + 'px; }';
96+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.width + 'px; }';
9797
}
9898
});
9999

@@ -119,7 +119,7 @@
119119
canvasWidth += colWidth;
120120
column.drawnWidth = colWidth;
121121

122-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
122+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
123123

124124
// Remove this element from the percent array so it's not processed below
125125
percentArray.splice(i, 1);
@@ -132,7 +132,7 @@
132132
canvasWidth += colWidth;
133133
column.drawnWidth = colWidth;
134134

135-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
135+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
136136

137137
// Remove this element from the percent array so it's not processed below
138138
percentArray.splice(i, 1);
@@ -147,7 +147,7 @@
147147

148148
column.drawnWidth = colWidth;
149149

150-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
150+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
151151
});
152152
}
153153

@@ -169,7 +169,7 @@
169169
canvasWidth += colWidth;
170170
column.drawnWidth = colWidth;
171171

172-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
172+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
173173

174174
lastColumn = column;
175175

@@ -185,7 +185,7 @@
185185
canvasWidth += colWidth;
186186
column.drawnWidth = colWidth;
187187

188-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
188+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
189189

190190
// Remove this element from the percent array so it's not processed below
191191
asterisksArray.splice(i, 1);
@@ -202,10 +202,32 @@
202202

203203
column.drawnWidth = colWidth;
204204

205-
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
205+
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + colWidth + 'px; }';
206206
});
207207
}
208208

209+
210+
// If the grid width didn't divide evenly into the column widths and we have pixels left over, dole them out to the columns one by one to make everything fit
211+
var leftoverWidth = uiGridCtrl.grid.gridWidth - parseInt(canvasWidth, 10);
212+
213+
if (leftoverWidth > 0 && canvasWidth > 0) {
214+
var remFn = function (column) {
215+
if (leftoverWidth > 0) {
216+
column.drawnWidth = column.drawnWidth + 1;
217+
canvasWidth = canvasWidth + 1;
218+
leftoverWidth--;
219+
}
220+
};
221+
while (leftoverWidth > 0) {
222+
uiGridCtrl.grid.columns.forEach(remFn);
223+
}
224+
}
225+
226+
// Build the CSS
227+
uiGridCtrl.grid.columns.forEach(function (column) {
228+
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.drawnWidth + 'px; }';
229+
});
230+
209231
$scope.columnStyles = ret;
210232

211233
uiGridCtrl.grid.canvasWidth = parseInt(canvasWidth, 10);

0 commit comments

Comments
 (0)