Skip to content

Commit bda48aa

Browse files
committed
fix(columns): don't reset resized columns on data reset by default
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
1 parent e6bc300 commit bda48aa

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

src/features/resize-columns/js/ui-grid-column-resizer.js

+2
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@
430430

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

434435
refreshCanvas(xDiff);
435436

@@ -542,6 +543,7 @@
542543

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

546548
refreshCanvas(xDiff);
547549

src/js/core/factories/GridColumn.js

+30-27
Original file line numberDiff line numberDiff line change
@@ -427,39 +427,42 @@ angular.module('ui.grid')
427427

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

430-
var colDefWidth = colDef.width;
431-
var parseErrorMsg = "Cannot parse column width '" + colDefWidth + "' for column named '" + colDef.name + "'";
432-
433-
if (!angular.isString(colDefWidth) && !angular.isNumber(colDefWidth)) {
434-
self.width = '*';
435-
} else if (angular.isString(colDefWidth)) {
436-
// See if it ends with a percent
437-
if (gridUtil.endsWith(colDefWidth, '%')) {
438-
// If so we should be able to parse the non-percent-sign part to a number
439-
var percentStr = colDefWidth.replace(/%/g, '');
440-
var percent = parseInt(percentStr, 10);
441-
if (isNaN(percent)) {
430+
if (!angular.isNumber(self.width) || !self.hasCustomWidth || colDef.allowCustomWidthOverride) {
431+
var colDefWidth = colDef.width;
432+
var parseErrorMsg = "Cannot parse column width '" + colDefWidth + "' for column named '" + colDef.name + "'";
433+
self.hasCustomWidth = false;
434+
435+
if (!angular.isString(colDefWidth) && !angular.isNumber(colDefWidth)) {
436+
self.width = '*';
437+
} else if (angular.isString(colDefWidth)) {
438+
// See if it ends with a percent
439+
if (gridUtil.endsWith(colDefWidth, '%')) {
440+
// If so we should be able to parse the non-percent-sign part to a number
441+
var percentStr = colDefWidth.replace(/%/g, '');
442+
var percent = parseInt(percentStr, 10);
443+
if (isNaN(percent)) {
444+
throw new Error(parseErrorMsg);
445+
}
446+
self.width = colDefWidth;
447+
}
448+
// And see if it's a number string
449+
else if (colDefWidth.match(/^(\d+)$/)) {
450+
self.width = parseInt(colDefWidth.match(/^(\d+)$/)[1], 10);
451+
}
452+
// Otherwise it should be a string of asterisks
453+
else if (colDefWidth.match(/^\*+$/)) {
454+
self.width = colDefWidth;
455+
}
456+
// No idea, throw an Error
457+
else {
442458
throw new Error(parseErrorMsg);
443459
}
444-
self.width = colDefWidth;
445-
}
446-
// And see if it's a number string
447-
else if (colDefWidth.match(/^(\d+)$/)) {
448-
self.width = parseInt(colDefWidth.match(/^(\d+)$/)[1], 10);
449460
}
450-
// Otherwise it should be a string of asterisks
451-
else if (colDefWidth.match(/^\*+$/)) {
452-
self.width = colDefWidth;
453-
}
454-
// No idea, throw an Error
461+
// Is a number, use it as the width
455462
else {
456-
throw new Error(parseErrorMsg);
463+
self.width = colDefWidth;
457464
}
458465
}
459-
// Is a number, use it as the width
460-
else {
461-
self.width = colDefWidth;
462-
}
463466

464467
self.minWidth = !colDef.minWidth ? 30 : colDef.minWidth;
465468
self.maxWidth = !colDef.maxWidth ? 9000 : colDef.maxWidth;

0 commit comments

Comments
 (0)