Skip to content

Commit

Permalink
fix(Column Resizing): Don't update widths in colDef
Browse files Browse the repository at this point in the history
column.drawnWidth wasn't being updated after column resizing. This change
does two things:

1. Prevent updating column.width in `updateColumnDef` if it already has a
value set and the value is a number. If the column has a width that's a
number then we're assuming the user has said "I want it to be this wide"
and we shouldn't override that.
2. In the column resizer feature, update column.width, not colDef.width.
  • Loading branch information
c0bra committed Oct 3, 2014
1 parent 9e022ba commit 7454408
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/features/resize-columns/js/ui-grid-column-resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@

var colDef = column.colDef;
if (!colDef.width || (angular.isString(colDef.width) && (colDef.width.indexOf('*') !== -1 || colDef.width.indexOf('%') !== -1))) {
colDef.width = column.drawnWidth;
column.width = column.drawnWidth;
}
});
}
Expand Down Expand Up @@ -384,7 +384,7 @@
newWidth = col.colDef.maxWidth;
}

col.colDef.width = newWidth;
col.width = newWidth;

// All other columns because fixed to their drawn width, if they aren't already
resizeAroundColumn(col);
Expand Down Expand Up @@ -488,7 +488,7 @@
maxWidth = col.colDef.maxWidth;
}

col.colDef.width = parseInt(maxWidth, 10);
col.width = parseInt(maxWidth, 10);

// All other columns because fixed to their drawn width, if they aren't already
resizeAroundColumn(col);
Expand Down
50 changes: 26 additions & 24 deletions src/js/core/factories/GridColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,35 +311,37 @@ angular.module('ui.grid')
var parseErrorMsg = "Cannot parse column width '" + colDef.width + "' for column named '" + colDef.name + "'";

// If width is not defined, set it to a single star
if (gridUtil.isNullOrUndefined(colDef.width)) {
self.width = '*';
}
else {
// If the width is not a number
if (!angular.isNumber(colDef.width)) {
// See if it ends with a percent
if (gridUtil.endsWith(colDef.width, '%')) {
// If so we should be able to parse the non-percent-sign part to a number
var percentStr = colDef.width.replace(/%/g, '');
var percent = parseInt(percentStr, 10);
if (isNaN(percent)) {
if (gridUtil.isNullOrUndefined(self.width) || !angular.isNumber(self.width)) {
if (gridUtil.isNullOrUndefined(colDef.width)) {
self.width = '*';
}
else {
// If the width is not a number
if (!angular.isNumber(colDef.width)) {
// See if it ends with a percent
if (gridUtil.endsWith(colDef.width, '%')) {
// If so we should be able to parse the non-percent-sign part to a number
var percentStr = colDef.width.replace(/%/g, '');
var percent = parseInt(percentStr, 10);
if (isNaN(percent)) {
throw new Error(parseErrorMsg);
}
self.width = colDef.width;
}
// And see if it's a number string
else if (colDef.width.match(/^(\d+)$/)) {
self.width = parseInt(colDef.width.match(/^(\d+)$/)[1], 10);
}
// Otherwise it should be a string of asterisks
else if (!colDef.width.match(/^\*+$/)) {
throw new Error(parseErrorMsg);
}
self.width = colDef.width;
}
// And see if it's a number string
else if (colDef.width.match(/^(\d+)$/)) {
self.width = parseInt(colDef.width.match(/^(\d+)$/)[1], 10);
}
// Otherwise it should be a string of asterisks
else if (!colDef.width.match(/^\*+$/)) {
throw new Error(parseErrorMsg);
// Is a number, use it as the width
else {
self.width = colDef.width;
}
}
// Is a number, use it as the width
else {
self.width = colDef.width;
}
}

// Remove this column from the grid sorting
Expand Down

0 comments on commit 7454408

Please sign in to comment.