Skip to content

Commit

Permalink
fix(uiGridColumns): Fix auto-incrementing of column names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ndudenhoeffer committed May 23, 2015
1 parent 363e4a5 commit a10f141
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
49 changes: 6 additions & 43 deletions src/js/core/factories/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,51 +983,14 @@ angular.module('ui.grid')
//field was required in 2.x. now name is required
if (colDef.name === undefined && colDef.field !== undefined) {
// See if the column name already exists:
var foundName = self.getColumn(colDef.field);

// If a column with this name already exists, we will add an incrementing number to the end of the new column name
if (foundName) {
// Search through the columns for names in the format: <name><1, 2 ... N>, i.e. 'Age1, Age2, Age3',
var nameRE = new RegExp('^' + colDef.field + '(\\d+)$', 'i');

var foundColumns = self.columns.filter(function (column) {
// Test against the displayName, as that's what'll have the incremented number
return nameRE.test(column.displayName);
})
// Sort the found columns by the end-number
.sort(function (a, b) {
if (a === b) {
return 0;
}
else {
var numA = a.displayName.match(nameRE)[1];
var numB = b.displayName.match(nameRE)[1];

return parseInt(numA, 10) > parseInt(numB, 10) ? 1 : -1;
}
});

// Not columns found, so start with number "2"
if (foundColumns.length === 0) {
colDef.name = colDef.field + '2';
}
else {
// Get the number from the final column
var lastNum = foundColumns[foundColumns.length-1].displayName.match(nameRE)[1];

// Make sure to parse to an int
lastNum = parseInt(lastNum, 10);

// Add 1 to the number from the last column and tack it on to the field to be the name for this new column
colDef.name = colDef.field + (lastNum + 1);
}
}
// ... otherwise just use the field as the column name
else {
colDef.name = colDef.field;
var newName = colDef.field,
counter = 2;
while (self.getColumn(newName)) {
newName = colDef.field + counter.toString();
counter++;
}
colDef.name = newName;
}

};

// 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
Expand Down
35 changes: 27 additions & 8 deletions test/unit/core/factories/GridColumn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,47 @@ describe('GridColumn factory', function () {

buildCols();

expect(grid.columns[0].name).toEqual('age');
expect(grid.columns[1].name).toEqual('name');
expect(grid.columns[2].name).toEqual('name2');
expect(grid.columns[3].name).toEqual('name3');
});

it('should not change the displayNames if they are provided', function () {
var cols = [
{ field: 'age' },
{ field: 'name', displayName:'First Name' },
{ field: 'name', displayName:'First Name' },
{ field: 'name', displayName:'First Name' }
];

grid.options.columnDefs = cols;

buildCols();

expect(grid.columns[0].displayName).toEqual('Age');
expect(grid.columns[1].displayName).toEqual('Name');
expect(grid.columns[2].displayName).toEqual('Name2');
expect(grid.columns[3].displayName).toEqual('Name3');
expect(grid.columns[1].displayName).toEqual('First Name');
expect(grid.columns[2].displayName).toEqual('First Name');
expect(grid.columns[3].displayName).toEqual('First Name');

});

it('should account for existing incremented names', function () {
var cols = [
{ field: 'age' },
{ field: 'name' },
{ field: 'name', name: 'Name3' },
{ field: 'name', name: 'name3' },
{ field: 'name' }
];

grid.options.columnDefs = cols;

buildCols();

expect(grid.columns[0].displayName).toEqual('Age');
expect(grid.columns[1].displayName).toEqual('Name');
expect(grid.columns[2].displayName).toEqual('Name3');
expect(grid.columns[3].displayName).toEqual('Name4');
expect(grid.columns[0].name).toEqual('age');
expect(grid.columns[1].name).toEqual('name');
expect(grid.columns[2].name).toEqual('name3');
expect(grid.columns[3].name).toEqual('name2');
});
});
});
Expand Down

0 comments on commit a10f141

Please sign in to comment.