Skip to content

Commit d43517c

Browse files
fix: only use trackByProp on row level with strict type
fix `DatatableComponent.trackByProp` which always returned undefined on group level by using key as index for group. BREAKING CHANGE: `DatatableComponent.trackByProp` input now enforce strict type check as key of row. Before: // Even though name is not a valid prop on rows it is allowed to be used with `trackByProp`. ``` <ngx-datatable trackByProp="'name'" [rows]="[{id: 1}, {id: 2}]" > ``` After: Typescript would give compilation error as name is not know property in rows.
1 parent babdc44 commit d43517c

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

projects/ngx-datatable/src/lib/components/body/body.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
278278
readonly groupHeader = input<DatatableGroupHeaderDirective>();
279279
readonly selectCheck = input<(value: TRow, index: number, array: TRow[]) => boolean>();
280280
readonly displayCheck = input<(row: TRow, column: TableColumnInternal, value?: any) => boolean>();
281-
readonly trackByProp = input<string>();
281+
readonly trackByProp = input<keyof TRow>();
282282
readonly rowClass = input<(row: TRow) => string | Record<string, boolean>>();
283283
readonly groupedRows = input<Group<TRow>[]>();
284284
// TODO: Find a better way to handle default expansion state with signal input
@@ -394,8 +394,8 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
394394
return index;
395395
}
396396
const trackByProp = this.trackByProp();
397-
if (trackByProp && row) {
398-
return (row as any)[trackByProp];
397+
if (trackByProp && row && this.isRow(row)) {
398+
return row[trackByProp];
399399
} else if (row && this.isGroup(row)) {
400400
return row.key ?? index;
401401
} else {

projects/ngx-datatable/src/lib/components/datatable.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export class DatatableComponent<TRow extends Row = any>
420420
* Property to which you can use for custom tracking of rows.
421421
* Example: 'name'
422422
*/
423-
@Input() trackByProp?: string;
423+
@Input() trackByProp?: keyof TRow;
424424

425425
/**
426426
* Property to which you can use for determining select all

0 commit comments

Comments
 (0)