Skip to content

Commit

Permalink
fix(columns): don't reset resized columns on data reset by default
Browse files Browse the repository at this point in the history
Previously, if a user resized a column and the data were reset, the column size
would also be reset.  Instead, disable this behavior by default and add a flag
(`allowCustomWidthOverride`) to allow it.
Code originally taken from mage-eag@966c4d9

Fixes #4005
  • Loading branch information
SomeKittens committed Aug 15, 2015
1 parent e6bc300 commit bda48aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/features/resize-columns/js/ui-grid-column-resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@

// check we're not outside the allowable bounds for this column
col.width = constrainWidth(col, newWidth);
col.hasCustomWidth = true;

refreshCanvas(xDiff);

Expand Down Expand Up @@ -542,6 +543,7 @@

// check we're not outside the allowable bounds for this column
col.width = constrainWidth(col, maxWidth);
col.hasCustomWidth = true;

refreshCanvas(xDiff);

Expand Down
57 changes: 30 additions & 27 deletions src/js/core/factories/GridColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,39 +427,42 @@ angular.module('ui.grid')

self.displayName = (colDef.displayName === undefined) ? gridUtil.readableColumnName(colDef.name) : colDef.displayName;

var colDefWidth = colDef.width;
var parseErrorMsg = "Cannot parse column width '" + colDefWidth + "' for column named '" + colDef.name + "'";

if (!angular.isString(colDefWidth) && !angular.isNumber(colDefWidth)) {
self.width = '*';
} else if (angular.isString(colDefWidth)) {
// See if it ends with a percent
if (gridUtil.endsWith(colDefWidth, '%')) {
// If so we should be able to parse the non-percent-sign part to a number
var percentStr = colDefWidth.replace(/%/g, '');
var percent = parseInt(percentStr, 10);
if (isNaN(percent)) {
if (!angular.isNumber(self.width) || !self.hasCustomWidth || colDef.allowCustomWidthOverride) {
var colDefWidth = colDef.width;
var parseErrorMsg = "Cannot parse column width '" + colDefWidth + "' for column named '" + colDef.name + "'";
self.hasCustomWidth = false;

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

self.minWidth = !colDef.minWidth ? 30 : colDef.minWidth;
self.maxWidth = !colDef.maxWidth ? 9000 : colDef.maxWidth;
Expand Down

0 comments on commit bda48aa

Please sign in to comment.