Skip to content

Commit

Permalink
fix(uiGridHeader): handle leftover pixels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
c0bra committed Mar 13, 2014
1 parent 2b55324 commit 64941b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/features/resize-columns/test/resizeColumns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ describe('ui.grid.resizeColumns', function () {
});

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

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

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

firstResizer.trigger('dblclick');

Expand Down
36 changes: 29 additions & 7 deletions src/js/core/directives/ui-grid-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

column.drawnWidth = column.width;

ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.width + 'px; }';
// ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.width + 'px; }';
}
});

Expand All @@ -119,7 +119,7 @@
canvasWidth += colWidth;
column.drawnWidth = colWidth;

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

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

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

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

column.drawnWidth = colWidth;

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

Expand All @@ -169,7 +169,7 @@
canvasWidth += colWidth;
column.drawnWidth = colWidth;

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

lastColumn = column;

Expand All @@ -185,7 +185,7 @@
canvasWidth += colWidth;
column.drawnWidth = colWidth;

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

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

column.drawnWidth = colWidth;

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


// 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
var leftoverWidth = uiGridCtrl.grid.gridWidth - parseInt(canvasWidth, 10);

if (leftoverWidth > 0 && canvasWidth > 0) {
var remFn = function (column) {
if (leftoverWidth > 0) {
column.drawnWidth = column.drawnWidth + 1;
canvasWidth = canvasWidth + 1;
leftoverWidth--;
}
};
while (leftoverWidth > 0) {
uiGridCtrl.grid.columns.forEach(remFn);
}
}

// Build the CSS
uiGridCtrl.grid.columns.forEach(function (column) {
ret = ret + ' .grid' + uiGridCtrl.grid.id + ' .col' + column.index + ' { width: ' + column.drawnWidth + 'px; }';
});

$scope.columnStyles = ret;

uiGridCtrl.grid.canvasWidth = parseInt(canvasWidth, 10);
Expand Down

1 comment on commit 64941b3

@sheerun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what they call pixel-perfect ;)

Please sign in to comment.