Skip to content

Commit 423016f

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 76d8825 commit 423016f

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
@@ -250,7 +250,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
250250
@Input() groupHeader?: DatatableGroupHeaderDirective;
251251
@Input() selectCheck?: (value: TRow, index: number, array: TRow[]) => boolean;
252252
@Input() displayCheck?: (row: TRow, column: TableColumnInternal, value?: any) => boolean;
253-
@Input() trackByProp?: string;
253+
@Input() trackByProp?: keyof TRow;
254254
@Input() rowClass?: (row: TRow) => string | Record<string, boolean>;
255255
@Input() groupedRows?: Group<TRow>[];
256256
@Input() groupExpansionDefault?: boolean;
@@ -419,8 +419,8 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
419419
if (this.ghostLoadingIndicator) {
420420
return index;
421421
}
422-
if (this.trackByProp && row) {
423-
return (row as any)[this.trackByProp];
422+
if (this.trackByProp && row && this.isRow(row)) {
423+
return row[this.trackByProp];
424424
} else if (row && this.isGroup(row)) {
425425
return row.key ?? index;
426426
} 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)