Skip to content

Commit

Permalink
critical issue with Cell.newValue - fixes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
uap-universe committed Oct 24, 2023
1 parent 7d8bbac commit 2891780
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This document lists the changes introduced by this fork.
## Version 3.1.0

* Add new setting `valueCreateFunction`
* Fix a critical issue with the inner state of an edited cell
* The `strict` setting for `list` default filters is no longer overriding a custom provided filter function
* Fix that the default sort direction for all columns is `asc` instead of "no sort"

Expand Down
1 change: 1 addition & 0 deletions projects/angular2-smart-table/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This document lists the changes introduced by this fork.
## Version 3.1.0

* Add new setting `valueCreateFunction`
* Fix a critical issue with the inner state of an edited cell
* The `strict` setting for `list` default filters is no longer overriding a custom provided filter function
* Fix that the default sort direction for all columns is `asc` instead of "no sort"

Expand Down
34 changes: 17 additions & 17 deletions projects/angular2-smart-table/src/lib/lib/data-set/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ import {Row} from './row';
export class Cell {

private cachedValue: unknown;
private cachedPreparedValue: string;
private cachedPreparedValue: string = '';

private newValue: string;
private newValue: unknown;

constructor(protected value: unknown, protected row: Row, protected column: Column) {
this.cachedValue = this.value;
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 = '';
}
this.resetValue();
}

getColumn(): Column {
Expand All @@ -32,15 +24,23 @@ export class Cell {
* Gets the value (after post-processing with valuePrepareFunction).
*/
getValue(): string {
if (this.cachedValue === this.value) return this.cachedPreparedValue;
this.cachedPreparedValue = this.getPreparedValue();
this.cachedValue = this.value;
if (this.cachedValue !== this.value) {
this.cachedPreparedValue = this.getPreparedValue();
this.cachedValue = this.value;
}
return this.cachedPreparedValue;
}

protected getPreparedValue(): string {
const prepare = this.column.valuePrepareFunction ?? ((v) => (v?.toString()??''));
return prepare.call(null, this.value, this);
try {
const prepare = this.column.valuePrepareFunction ?? ((v) => (v?.toString() ?? ''));
return prepare.call(null, this.value, this);
} catch (_) {
console.error(`The valuePrepareFunction of column ${this.column.id} threw an error. Using simple toString() as fallback.`);
console.error('Please check the implementation of valuePrepareFunction.');
console.error('If this error was raised when creating a new row, please also check the implementation of valueCreateFunction.');
return this.value?.toString() ?? '';
}
}

/**
Expand Down Expand Up @@ -81,7 +81,7 @@ export class Cell {

resetValue(): void {
this.cachedValue = this.value;
this.newValue = this.value;
this.cachedPreparedValue = this.getPreparedValue();
this.newValue = this.cachedPreparedValue;
}
}

0 comments on commit 2891780

Please sign in to comment.