Skip to content

Commit

Permalink
Merge pull request #1750 from dlascarez/bug/BUG-1748
Browse files Browse the repository at this point in the history
Create an id property, this new property will be used as unique to in…
  • Loading branch information
l-lin authored Sep 22, 2023
2 parents f789275 + f4a5598 commit 605015e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/angular-datatables.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ export class DataTableDirective implements OnDestroy, OnInit {
reject('Both the table and dtOptions cannot be empty');
return;
}

// Set a column unique
if (resolvedDTOptions.columns) {
resolvedDTOptions.columns.forEach(col => {
if ((col.id ?? '').trim() === '') {
col.id = this.getColumnUniqueId();
}
});
}

// Using setTimeout as a "hack" to be "part" of NgZone
setTimeout(() => {
// Assign DT properties here
Expand Down Expand Up @@ -108,7 +118,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
const pipe = el.ngPipeInstance;
const pipeArgs = el.ngPipeArgs || [];
// find index of column using `data` attr
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
const i = columns.filter(c => c.visible !== false).findIndex(e => e.id === el.id);
// get <td> element which holds data using index
const rowFromCol = row.childNodes.item(i);
// Transform data with Pipe and PipeArgs
Expand All @@ -125,7 +135,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
colsWithTemplate.forEach(el => {
const { ref, context } = el.ngTemplateRef;
// get <td> element which holds data using index
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
const i = columns.filter(c => c.visible !== false).findIndex(e => e.id === el.id);
const cellFromIndex = row.childNodes.item(i);
// reset cell before applying transform
$(cellFromIndex).html('');
Expand All @@ -138,4 +148,17 @@ export class DataTableDirective implements OnDestroy, OnInit {
this.renderer.appendChild(cellFromIndex, instance.rootNodes[0]);
});
}

private getColumnUniqueId(): string {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

for (let i = 0; i < 6; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}

return result.trim();
}

}
2 changes: 2 additions & 0 deletions src/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface ADTSettings extends DataTables.Settings {
columns?: ADTColumns[];
}
export interface ADTColumns extends DataTables.ColumnSettings {
/** Define the column's unique identifier */
id?: string;
/** Set instance of Angular pipe to transform the data of particular column */
ngPipeInstance?: PipeTransform;
/** Define the arguments for the tranform method of the pipe, to change its behavior */
Expand Down

0 comments on commit 605015e

Please sign in to comment.