@@ -75,6 +75,16 @@ export class DataTableDirective implements OnDestroy, OnInit {
75
75
reject ( 'Both the table and dtOptions cannot be empty' ) ;
76
76
return ;
77
77
}
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
+
78
88
// Using setTimeout as a "hack" to be "part" of NgZone
79
89
setTimeout ( ( ) => {
80
90
// Assign DT properties here
@@ -108,7 +118,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
108
118
const pipe = el . ngPipeInstance ;
109
119
const pipeArgs = el . ngPipeArgs || [ ] ;
110
120
// 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 ) ;
112
122
// get <td> element which holds data using index
113
123
const rowFromCol = row . childNodes . item ( i ) ;
114
124
// Transform data with Pipe and PipeArgs
@@ -125,7 +135,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
125
135
colsWithTemplate . forEach ( el => {
126
136
const { ref, context } = el . ngTemplateRef ;
127
137
// 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 ) ;
129
139
const cellFromIndex = row . childNodes . item ( i ) ;
130
140
// reset cell before applying transform
131
141
$ ( cellFromIndex ) . html ( '' ) ;
@@ -138,4 +148,17 @@ export class DataTableDirective implements OnDestroy, OnInit {
138
148
this . renderer . appendChild ( cellFromIndex , instance . rootNodes [ 0 ] ) ;
139
149
} ) ;
140
150
}
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
+
141
164
}
0 commit comments