Skip to content

Commit

Permalink
fix(select-input): Add support custom idField and titleField
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Mar 10, 2019
1 parent 0eae3f0 commit 3e2a7eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Expand Up @@ -9,8 +9,8 @@
[okText]="yesTitle | translate">
<ion-select-option
*ngFor="let item of items; trackBy: trackByFn;"
[value]="item.id">
{{(item.title || item) | customTranslate:item}}
[value]="item[idField]">
{{(item[titleField] || item) | customTranslate:item}}
</ion-select-option>
</ion-select>
</ng-container>
Expand Up @@ -28,7 +28,9 @@ export class SelectInputComponent implements ControlValueAccessor, OnDestroy, On
@Input()
multiple = false;
@Input()
titleField = undefined;
idField = 'id';
@Input()
titleField = 'title';
@Input()
@BindObservable()
items: SelectInput[] = undefined;
Expand All @@ -52,7 +54,7 @@ export class SelectInputComponent implements ControlValueAccessor, OnDestroy, On
if (ids !== undefined && !Array.isArray(ids)) {
ids = [ids];
}
const selectedItems = this.items.filter(item => ids.filter(id => item.id === id).length > 0);
const selectedItems = this.items.filter(item => ids.filter(id => item[this.idField] === id).length > 0);
if (this.multiple) {
this._onChange(selectedItems);
} else {
Expand All @@ -64,7 +66,7 @@ export class SelectInputComponent implements ControlValueAccessor, OnDestroy, On
if (value && !Array.isArray(value)) {
value = [value];
}
this.selectedIds = value ? (value as SelectInput[]).map(item => item.id) : [];
this.selectedIds = value ? (value as SelectInput[]).map(item => item[this.idField]) : [];
}
registerOnChange(fn: any): void {
this._onChange = fn;
Expand All @@ -75,8 +77,8 @@ export class SelectInputComponent implements ControlValueAccessor, OnDestroy, On
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
trackByFn(index: any, item: { id: any; }) {
return item.id;
trackByFn(index: any, item: any) {
return item[this.idField];
}
_onChange = (value: any) => { };
_onTouched = () => { };
Expand Down

0 comments on commit 3e2a7eb

Please sign in to comment.