@@ -622,19 +622,18 @@ angular.module('ui.grid')
622
622
var columnCache = self . columns . slice ( 0 ) ;
623
623
624
624
// We need to allow for the "row headers" when mapping from the column defs array to the columns array
625
- // If we have a row header in columns[0] and don't account for it we'll overwrite it with the column in columnDefs[0]
626
- var rowHeaderOffset = self . rowHeaderColumns . length ;
625
+ // If we have a row header in columns[0] and don't account for it we'll overwrite it with the column in columnDefs[0]
627
626
628
627
// Go through all the column defs
629
628
for ( i = 0 ; i < self . options . columnDefs . length ; i ++ ) {
630
629
// If the column at this index has a different name than the column at the same index in the column defs...
631
- if ( self . columns [ i + rowHeaderOffset ] . name !== self . options . columnDefs [ i ] . name ) {
630
+ if ( self . columns [ i + headerOffset ] . name !== self . options . columnDefs [ i ] . name ) {
632
631
// Replace the one in the cache with the appropriate column
633
- columnCache [ i + rowHeaderOffset ] = self . getColumn ( self . options . columnDefs [ i ] . name ) ;
632
+ columnCache [ i + headerOffset ] = self . getColumn ( self . options . columnDefs [ i ] . name ) ;
634
633
}
635
634
else {
636
635
// Otherwise just copy over the one from the initial columns
637
- columnCache [ i + rowHeaderOffset ] = self . columns [ i + rowHeaderOffset ] ;
636
+ columnCache [ i + headerOffset ] = self . columns [ i + headerOffset ] ;
638
637
}
639
638
}
640
639
@@ -738,14 +737,59 @@ angular.module('ui.grid')
738
737
* validates that name or field is present
739
738
*/
740
739
Grid . prototype . preprocessColDef = function preprocessColDef ( colDef ) {
740
+ var self = this ;
741
+
741
742
if ( ! colDef . field && ! colDef . name ) {
742
743
throw new Error ( 'colDef.name or colDef.field property is required' ) ;
743
744
}
744
745
745
746
//maintain backwards compatibility with 2.x
746
747
//field was required in 2.x. now name is required
747
748
if ( colDef . name === undefined && colDef . field !== undefined ) {
748
- colDef . name = colDef . field ;
749
+ // See if the column name already exists:
750
+ var foundName = self . getColumn ( colDef . field ) ;
751
+
752
+ // If a column with this name already exists, we will add an incrementing number to the end of the new column name
753
+ if ( foundName ) {
754
+ // Search through the columns for names in the format: <name><1, 2 ... N>, i.e. 'Age1, Age2, Age3',
755
+ var nameRE = new RegExp ( '^' + colDef . field + '(\\d+)$' , 'i' ) ;
756
+
757
+ var foundColumns = self . columns . filter ( function ( column ) {
758
+ // Test against the displayName, as that's what'll have the incremented number
759
+ return nameRE . test ( column . displayName ) ;
760
+ } )
761
+ // Sort the found columns by the end-number
762
+ . sort ( function ( a , b ) {
763
+ if ( a === b ) {
764
+ return 0 ;
765
+ }
766
+ else {
767
+ var numA = a . match ( nameRE ) [ 1 ] ;
768
+ var numB = b . match ( nameRE ) [ 1 ] ;
769
+
770
+ return parseInt ( numA , 10 ) > parseInt ( numB , 10 ) ? 1 : - 1 ;
771
+ }
772
+ } ) ;
773
+
774
+ // Not columns found, so start with number "2"
775
+ if ( foundColumns . length === 0 ) {
776
+ colDef . name = colDef . field + '2' ;
777
+ }
778
+ else {
779
+ // Get the number from the final column
780
+ var lastNum = foundColumns [ foundColumns . length - 1 ] . displayName . match ( nameRE ) [ 1 ] ;
781
+
782
+ // Make sure to parse to an int
783
+ lastNum = parseInt ( lastNum , 10 ) ;
784
+
785
+ // Add 1 to the number from the last column and tack it on to the field to be the name for this new column
786
+ colDef . name = colDef . field + ( lastNum + 1 ) ;
787
+ }
788
+ }
789
+ // ... otherwise just use the field as the column name
790
+ else {
791
+ colDef . name = colDef . field ;
792
+ }
749
793
}
750
794
751
795
} ;
0 commit comments