Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 605015e

Browse files
authored
Merge pull request #1750 from dlascarez/bug/BUG-1748
Create an id property, this new property will be used as unique to in…
2 parents f789275 + f4a5598 commit 605015e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/angular-datatables.directive.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/models/settings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export interface ADTSettings extends DataTables.Settings {
44
columns?: ADTColumns[];
55
}
66
export interface ADTColumns extends DataTables.ColumnSettings {
7+
/** Define the column's unique identifier */
8+
id?: string;
79
/** Set instance of Angular pipe to transform the data of particular column */
810
ngPipeInstance?: PipeTransform;
911
/** Define the arguments for the tranform method of the pipe, to change its behavior */

0 commit comments

Comments
 (0)