From d6cc0adacce0b8f3a7c90ec3c7491e07ac86b8bf Mon Sep 17 00:00:00 2001 From: Mike Becker Date: Wed, 9 Aug 2023 17:12:53 +0200 Subject: [PATCH] regression: valuePrepareFunction is applied to undefined values from the "newRow" row It is crazy that the "newRow" row is created before the user clicked on the ADD button. This regression was a lot harder to debug that it should be... phew... --- .../angular2-smart-table/src/lib/lib/data-set/cell.ts | 10 ++++++++-- .../src/lib/lib/data-set/data-set.ts | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/projects/angular2-smart-table/src/lib/lib/data-set/cell.ts b/projects/angular2-smart-table/src/lib/lib/data-set/cell.ts index 97a166c6..d1cb901c 100644 --- a/projects/angular2-smart-table/src/lib/lib/data-set/cell.ts +++ b/projects/angular2-smart-table/src/lib/lib/data-set/cell.ts @@ -10,8 +10,14 @@ export class Cell { constructor(protected value: unknown, protected row: Row, protected column: Column) { this.cachedValue = this.value; - this.cachedPreparedValue = this.getPreparedValue(); - this.newValue = this.cachedPreparedValue; + if (this.row.index >= 0) { + this.cachedPreparedValue = this.getPreparedValue(); + this.newValue = this.cachedPreparedValue; + } else { + // we must not call the valuePrepareFunction on freshly created rows that do not contain defined data + this.cachedPreparedValue = ''; + this.newValue = ''; + } } getColumn(): Column { diff --git a/projects/angular2-smart-table/src/lib/lib/data-set/data-set.ts b/projects/angular2-smart-table/src/lib/lib/data-set/data-set.ts index 65576e7a..7c97491e 100644 --- a/projects/angular2-smart-table/src/lib/lib/data-set/data-set.ts +++ b/projects/angular2-smart-table/src/lib/lib/data-set/data-set.ts @@ -16,7 +16,7 @@ export class DataSet { constructor(data: Array = [], protected columnSettings: IColumns) { this.createColumns(columnSettings); this.setData(data); - + // TODO: fix that the "new row" is always created, even when the table is not even configured to add new rows this.createNewRow(); } @@ -171,6 +171,9 @@ export class DataSet { } createNewRow() { + // TODO: the empty object is invalid data in general and is very likely breaking almost every other function + // in particular, custom valuePrepareFunction can explode (see the related hack in the Cell's constructor) + // we should some day fix this by defining a valueCreateFunction that can create a reasonable default object this.newRow = new Row(-1, {}, this); this.newRow.isInEditing = true; }