Skip to content

Commit a10f141

Browse files
author
ndudenhoeffer
committed
fix(uiGridColumns): Fix auto-incrementing of column names
When generating unique column names, do not consider the displayName. Increment column name property only, using the field as the base. Increment displayName property only if it is auto-generated, displaying any provided displayName unchanged. Fixes: #3453
1 parent 363e4a5 commit a10f141

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

src/js/core/factories/Grid.js

+6-43
Original file line numberDiff line numberDiff line change
@@ -983,51 +983,14 @@ angular.module('ui.grid')
983983
//field was required in 2.x. now name is required
984984
if (colDef.name === undefined && colDef.field !== undefined) {
985985
// See if the column name already exists:
986-
var foundName = self.getColumn(colDef.field);
987-
988-
// If a column with this name already exists, we will add an incrementing number to the end of the new column name
989-
if (foundName) {
990-
// Search through the columns for names in the format: <name><1, 2 ... N>, i.e. 'Age1, Age2, Age3',
991-
var nameRE = new RegExp('^' + colDef.field + '(\\d+)$', 'i');
992-
993-
var foundColumns = self.columns.filter(function (column) {
994-
// Test against the displayName, as that's what'll have the incremented number
995-
return nameRE.test(column.displayName);
996-
})
997-
// Sort the found columns by the end-number
998-
.sort(function (a, b) {
999-
if (a === b) {
1000-
return 0;
1001-
}
1002-
else {
1003-
var numA = a.displayName.match(nameRE)[1];
1004-
var numB = b.displayName.match(nameRE)[1];
1005-
1006-
return parseInt(numA, 10) > parseInt(numB, 10) ? 1 : -1;
1007-
}
1008-
});
1009-
1010-
// Not columns found, so start with number "2"
1011-
if (foundColumns.length === 0) {
1012-
colDef.name = colDef.field + '2';
1013-
}
1014-
else {
1015-
// Get the number from the final column
1016-
var lastNum = foundColumns[foundColumns.length-1].displayName.match(nameRE)[1];
1017-
1018-
// Make sure to parse to an int
1019-
lastNum = parseInt(lastNum, 10);
1020-
1021-
// Add 1 to the number from the last column and tack it on to the field to be the name for this new column
1022-
colDef.name = colDef.field + (lastNum + 1);
1023-
}
1024-
}
1025-
// ... otherwise just use the field as the column name
1026-
else {
1027-
colDef.name = colDef.field;
986+
var newName = colDef.field,
987+
counter = 2;
988+
while (self.getColumn(newName)) {
989+
newName = colDef.field + counter.toString();
990+
counter++;
1028991
}
992+
colDef.name = newName;
1029993
}
1030-
1031994
};
1032995

1033996
// Return a list of items that exist in the `n` array but not the `o` array. Uses optional property accessors passed as third & fourth parameters

test/unit/core/factories/GridColumn.spec.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,47 @@ describe('GridColumn factory', function () {
114114

115115
buildCols();
116116

117+
expect(grid.columns[0].name).toEqual('age');
118+
expect(grid.columns[1].name).toEqual('name');
119+
expect(grid.columns[2].name).toEqual('name2');
120+
expect(grid.columns[3].name).toEqual('name3');
121+
});
122+
123+
it('should not change the displayNames if they are provided', function () {
124+
var cols = [
125+
{ field: 'age' },
126+
{ field: 'name', displayName:'First Name' },
127+
{ field: 'name', displayName:'First Name' },
128+
{ field: 'name', displayName:'First Name' }
129+
];
130+
131+
grid.options.columnDefs = cols;
132+
133+
buildCols();
134+
117135
expect(grid.columns[0].displayName).toEqual('Age');
118-
expect(grid.columns[1].displayName).toEqual('Name');
119-
expect(grid.columns[2].displayName).toEqual('Name2');
120-
expect(grid.columns[3].displayName).toEqual('Name3');
136+
expect(grid.columns[1].displayName).toEqual('First Name');
137+
expect(grid.columns[2].displayName).toEqual('First Name');
138+
expect(grid.columns[3].displayName).toEqual('First Name');
139+
121140
});
122141

123142
it('should account for existing incremented names', function () {
124143
var cols = [
125144
{ field: 'age' },
126145
{ field: 'name' },
127-
{ field: 'name', name: 'Name3' },
146+
{ field: 'name', name: 'name3' },
128147
{ field: 'name' }
129148
];
130149

131150
grid.options.columnDefs = cols;
132151

133152
buildCols();
134153

135-
expect(grid.columns[0].displayName).toEqual('Age');
136-
expect(grid.columns[1].displayName).toEqual('Name');
137-
expect(grid.columns[2].displayName).toEqual('Name3');
138-
expect(grid.columns[3].displayName).toEqual('Name4');
154+
expect(grid.columns[0].name).toEqual('age');
155+
expect(grid.columns[1].name).toEqual('name');
156+
expect(grid.columns[2].name).toEqual('name3');
157+
expect(grid.columns[3].name).toEqual('name2');
139158
});
140159
});
141160
});

0 commit comments

Comments
 (0)