+/**
+ * <eg-combobox [allowFreeText]="true" [entries]="comboboxEntryList"/>
+ * <!-- see also <eg-combobox-entry> -->
+ * </eg-combobox>
+ */
import {Component, OnInit, Input, Output, ViewChild, EventEmitter, ElementRef} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {map} from 'rxjs/operators/map';
// onChange() is NOT fired when applying the default value
@Input() startId: any;
+ // True if a default selection has been made.
+ defaultSelectionApplied: boolean;
+
@Input() set entries(el: ComboboxEntry[]) {
this.entrylist = el;
this.applySelection();
this.click$ = new Subject<string>();
this.onChange = new EventEmitter<ComboboxEntry>();
this.freeTextId = -1;
+ this.defaultSelectionApplied = false;
this.formatDisplayString = (result: ComboboxEntry) => {
return result.label.trim();
// Apply a default selection where needed
applySelection() {
+
+ if (this.defaultSelectionApplied
+ || !this.entrylist || this.entrylist.length === 0) {
+ // Avoid unnecessary processing. Also as the page renders,
+ // the entrylist may be empty at times, avoid any attempts
+ // to apply selection during that time
+ return;
+ }
+
if (this.startId) {
- const entry = this.entrylist.filter(e => e.id === entry.id)[0];
+ const entry =
+ this.entrylist.filter(e => e.id === this.startId)[0];
if (entry) {
this.selected = entry;
+ this.defaultSelectionApplied = true;
}
} else if (this.selectFirst) {
this.selected = this.entrylist[0];
+ this.defaultSelectionApplied = true;
}
}
// This only fires when an item in the list is selected, not when
// the value is cleared or free-text is used.
selectorChanged(selEvent: NgbTypeaheadSelectItemEvent) {
- console.log('selector changed');
this.onChange.emit(selEvent.item.id);
}
private toast: ToastService,
private printer: PrintService
) {
-
- this.taEntries = [];
}
ngOnInit() {
];
this.pcrud.retrieveAll('cmrcfld', {order_by:{cmrcfld: 'name'}})
- .subscribe(format =>
- this.taEntries.push({id: format.id(), label: format.name()}));
+ .subscribe(format => {
+ if (!this.taEntries) { this.taEntries = []; }
+ this.taEntries.push({id: format.id(), label: format.name()})
+ });
this.btSource.getRows = (pager: Pager, sort: any[]) => {