Skip to content

Commit

Permalink
fix(editors): fix serialization/deserilization in editors
Browse files Browse the repository at this point in the history
dateEditor was serializing/deserializing wrong value. Also need to check for empty date value because moment will throw an error. `singleSelect` and `multipleSelectEditor` were serializing the actual value (e.g number type) when it should be serializing to a string and `applyValue` should be deserializing from string to actual type (e.g. number).

closes #56
  • Loading branch information
jmzagorski committed May 16, 2018
1 parent 21215c4 commit 2ee03b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 10 additions & 2 deletions aurelia-slickgrid/src/aurelia-slickgrid/editors/dateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,22 @@ export class DateEditor implements Editor {
}

serializeValue() {
const domValue: string = this.$input.val();

if (!domValue) return '';

const outputFormat = mapMomentDateFormatWithFieldType(this.args.column.type || FieldType.dateIso);
const value = moment(this.defaultDate).format(outputFormat);
const value = moment(domValue).format(outputFormat);

return value;
}

applyValue(item: any, state: any) {
item[this.args.column.field] = state;
if (!state) return;

const outputFormat = mapMomentDateFormatWithFieldType(this.args.column.type || FieldType.dateIso);

item[this.args.column.field] = moment(state, outputFormat).toDate();
}

isValueChanged() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export class MultipleSelectEditor implements Editor {
}

applyValue(item: any, state: any): void {
item[this.args.column.field] = state;
item[this.args.column.field] = this.collection
.filter(c => state.indexOf(c[this.valueName].toString()) !== -1)
.map(c => c[this.valueName]);
}

destroy() {
Expand All @@ -141,7 +143,7 @@ export class MultipleSelectEditor implements Editor {
}

serializeValue(): any {
return this.currentValues;
return this.$editorElm.val();
}

focus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export class SingleSelectEditor implements Editor {
}

applyValue(item: any, state: any): void {
item[this.args.column.field] = state;
item[this.args.column.field] = findOrDefault(this.collection, (c: any) =>
c[this.valueName].toString() === state)[this.valueName];
}

destroy() {
Expand All @@ -119,7 +120,8 @@ export class SingleSelectEditor implements Editor {

loadValue(item: any): void {
// convert to string because that is how the DOM will return these values
this.defaultValue = item[this.columnDef.field].toString();
// make sure the prop exists first
this.defaultValue = item[this.columnDef.field] && item[this.columnDef.field].toString();

this.$editorElm.find('option').each((i: number, $e: any) => {
if (this.defaultValue === $e.value) {
Expand All @@ -133,7 +135,7 @@ export class SingleSelectEditor implements Editor {
}

serializeValue(): any {
return this.currentValue;
return this.$editorElm.val();
}

focus() {
Expand Down

0 comments on commit 2ee03b0

Please sign in to comment.