@@ -75,6 +75,16 @@ export class DataTableDirective implements OnDestroy, OnInit {
7575 reject ( 'Both the table and dtOptions cannot be empty' ) ;
7676 return ;
7777 }
78+
79+ // Set a column unique
80+ if ( resolvedDTOptions . columns ) {
81+ resolvedDTOptions . columns . forEach ( col => {
82+ if ( ( col . id ?? '' ) . trim ( ) === '' ) {
83+ col . id = this . getColumnUniqueId ( ) ;
84+ }
85+ } ) ;
86+ }
87+
7888 // Using setTimeout as a "hack" to be "part" of NgZone
7989 setTimeout ( ( ) => {
8090 // Assign DT properties here
@@ -108,7 +118,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
108118 const pipe = el . ngPipeInstance ;
109119 const pipeArgs = el . ngPipeArgs || [ ] ;
110120 // find index of column using `data` attr
111- const i = columns . filter ( c => c . visible !== false ) . findIndex ( e => e . data === el . data ) ;
121+ const i = columns . filter ( c => c . visible !== false ) . findIndex ( e => e . id === el . id ) ;
112122 // get <td> element which holds data using index
113123 const rowFromCol = row . childNodes . item ( i ) ;
114124 // Transform data with Pipe and PipeArgs
@@ -125,7 +135,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
125135 colsWithTemplate . forEach ( el => {
126136 const { ref, context } = el . ngTemplateRef ;
127137 // get <td> element which holds data using index
128- const i = columns . filter ( c => c . visible !== false ) . findIndex ( e => e . data === el . data ) ;
138+ const i = columns . filter ( c => c . visible !== false ) . findIndex ( e => e . id === el . id ) ;
129139 const cellFromIndex = row . childNodes . item ( i ) ;
130140 // reset cell before applying transform
131141 $ ( cellFromIndex ) . html ( '' ) ;
@@ -138,4 +148,17 @@ export class DataTableDirective implements OnDestroy, OnInit {
138148 this . renderer . appendChild ( cellFromIndex , instance . rootNodes [ 0 ] ) ;
139149 } ) ;
140150 }
151+
152+ private getColumnUniqueId ( ) : string {
153+ let result = '' ;
154+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
155+
156+ for ( let i = 0 ; i < 6 ; i ++ ) {
157+ const randomIndex = Math . floor ( Math . random ( ) * characters . length ) ;
158+ result += characters . charAt ( randomIndex ) ;
159+ }
160+
161+ return result . trim ( ) ;
162+ }
163+
141164}
0 commit comments