From 4276da655a283008c893d03c20593239e35f5d36 Mon Sep 17 00:00:00 2001 From: Damian Lascarez Rodriguez Date: Mon, 18 Sep 2023 10:58:53 -0600 Subject: [PATCH] Apply suggestions --- package.json | 2 +- src/angular-datatables.directive.ts | 21 +++++++++++++++++++-- src/models/settings.ts | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 64f68f7f..7587287e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-datatables", - "version": "16.0.1", + "version": "16.0.0", "description": "Angular directive for DataTables", "scripts": { "build": "npm run clean && npm run compile && npm run bundles && npm run schematics:build", diff --git a/src/angular-datatables.directive.ts b/src/angular-datatables.directive.ts index 657b9eb0..9825e944 100644 --- a/src/angular-datatables.directive.ts +++ b/src/angular-datatables.directive.ts @@ -75,9 +75,13 @@ 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); + resolvedDTOptions.columns.forEach(col => { + if ((col.id ?? '').trim() === '') { + col.id = this.getColumnUniqueId(); + } + }); // Using setTimeout as a "hack" to be "part" of NgZone setTimeout(() => { @@ -142,4 +146,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(); + } + } diff --git a/src/models/settings.ts b/src/models/settings.ts index e8fce041..c0834abd 100644 --- a/src/models/settings.ts +++ b/src/models/settings.ts @@ -4,8 +4,8 @@ export interface ADTSettings extends DataTables.Settings { columns?: ADTColumns[]; } export interface ADTColumns extends DataTables.ColumnSettings { - /** Define a column index */ - id?: number; + /** 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 */