Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit cf69f5d

Browse files
authored
Merge pull request #644 from ghiscoding/bugfix/select-editor-with-complex-object
fix(editors): Select Editor option to return flat data w/complex object
2 parents fe356fc + a3e6427 commit cf69f5d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/app/modules/angular-slickgrid/editors/__tests__/selectEditor.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TestBed } from '@angular/core/testing';
55
import { TranslateService, TranslateModule } from '@ngx-translate/core';
66
import { Editors } from '../index';
77
import { SelectEditor } from '../selectEditor';
8-
import { AutocompleteOption, Column, EditorArgs, EditorArguments, GridOption, FieldType, OperatorType } from '../../models';
8+
import { AutocompleteOption, Column, EditorArgs, EditorArguments, GridOption, FieldType, OperatorType, ColumnEditor } from '../../models';
99

1010
const containerId = 'demo-container';
1111

@@ -416,6 +416,19 @@ describe('SelectEditor', () => {
416416
expect(currentValue).toEqual({});
417417
});
418418

419+
it('should return flat value when using a dot (.) notation for complex object with a collection of option/label pair and using "serializeComplexValueFormat" as "flat"', () => {
420+
mockColumn.field = 'employee.gender';
421+
mockItemData = { id: 1, employee: { id: 24, gender: ['male', 'other'] }, isActive: true };
422+
(mockColumn.internalColumnEditor as ColumnEditor).serializeComplexValueFormat = 'flat';
423+
editor = new SelectEditor(editorArguments, true);
424+
editor.loadValue(mockItemData);
425+
const output = editor.serializeValue();
426+
const currentValue = editor.currentValue;
427+
428+
expect(output).toEqual(['male', 'other']);
429+
expect(currentValue).toEqual('');
430+
});
431+
419432
it('should return object value when using a dot (.) notation and we override the object path using "complexObjectPath" to find correct values', () => {
420433
mockColumn.field = 'employee.bio';
421434
mockItemData = { id: 1, employee: { id: 24, bio: { gender: ['male', 'other'] } }, isActive: true };

src/app/modules/angular-slickgrid/editors/selectEditor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ export class SelectEditor implements Editor {
207207

208208
// is the field a complex object, "address.streetNumber"
209209
const isComplexObject = fieldName && fieldName.indexOf('.') > 0;
210-
if (isComplexObject && typeof c === 'object') {
210+
const serializeComplexValueFormat = this.columnEditor && this.columnEditor.serializeComplexValueFormat || 'object';
211+
212+
if (isComplexObject && typeof c === 'object' && serializeComplexValueFormat === 'object') {
211213
return c;
212214
}
213215

@@ -242,8 +244,9 @@ export class SelectEditor implements Editor {
242244
// is the field a complex object, "address.streetNumber"
243245
const fieldName = this.columnDef && this.columnDef.field;
244246
const isComplexObject = fieldName && fieldName.indexOf('.') > 0;
247+
const serializeComplexValueFormat = this.columnEditor && this.columnEditor.serializeComplexValueFormat || 'object';
245248

246-
if (isComplexObject && typeof itemFound === 'object') {
249+
if (isComplexObject && typeof itemFound === 'object' && serializeComplexValueFormat === 'object') {
247250
return itemFound;
248251
} else if (itemFound && itemFound.hasOwnProperty(this.valueName)) {
249252
const labelText = itemFound[this.valueName];

src/app/modules/angular-slickgrid/models/columnEditor.interface.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ export interface ColumnEditor {
117117
*/
118118
required?: boolean;
119119

120+
/**
121+
* defaults to 'object', how do we want to serialize the editor value to the resulting dataContext object when using a complex object?
122+
* Currently only applies to Single/Multiple Select Editor.
123+
*
124+
* For example, if keep default "object" format and the selected value is { value: 2, label: 'Two' } then the end value will remain as an object, so { value: 2, label: 'Two' }.
125+
* On the other end, if we set "flat" format and the selected value is { value: 2, label: 'Two' } then the end value will be 2.
126+
*/
127+
serializeComplexValueFormat?: 'flat' | 'object';
128+
120129
/**
121130
* Title attribute that can be used in some Editors as tooltip (usually the "input" editors).
122131
*

0 commit comments

Comments
 (0)