Skip to content

Commit

Permalink
Create an id property, this new property will be used as unique to in…
Browse files Browse the repository at this point in the history
…dentify each column instead of "data"
  • Loading branch information
dlascarez committed Sep 18, 2023
1 parent f789275 commit faae6c0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-datatables",
"version": "16.0.0",
"version": "16.0.1",
"description": "Angular directive for DataTables",
"scripts": {
"build": "npm run clean && npm run compile && npm run bundles && npm run schematics:build",
Expand Down
8 changes: 6 additions & 2 deletions src/angular-datatables.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export class DataTableDirective implements OnDestroy, OnInit {
reject('Both the table and dtOptions cannot be empty');
return;
}

// Set a column unique
resolvedDTOptions.columns.forEach((col: ADTColumns, i: number) => col.id = i);

// Using setTimeout as a "hack" to be "part" of NgZone
setTimeout(() => {
// Assign DT properties here
Expand Down Expand Up @@ -108,7 +112,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 +129,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 Down
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 a column index */
id?: number;
/** 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 faae6c0

Please sign in to comment.