From 853e7b2408872bd3b95d88191b0908baf75e115c Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 29 Jul 2019 11:36:38 -0400 Subject: [PATCH] LP1831785 Combobox pcrud selector and pkey support Teach the PCRUD-driven combobox to use the IDL class' selector field as the sort and display field when no idlField value is provided. Teach the async source to use the pkey field of the IDL class instead of assuming the 'id' field. Tweak the sandbox example to fetch data for a class which uses a selector not called "name" and a pkey not called "id". Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg --- .../app/share/combobox/combobox.component.ts | 21 +++++++++++++++---- .../app/staff/sandbox/sandbox.component.html | 4 ++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts index 1fe37b141b..99206321cd 100644 --- a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts +++ b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts @@ -8,6 +8,7 @@ import {Observable, of, Subject} from 'rxjs'; import {map, tap, reduce, mergeMap, mapTo, debounceTime, distinctUntilChanged, merge, filter} from 'rxjs/operators'; import {NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap'; import {StoreService} from '@eg/core/store.service'; +import {IdlService} from '@eg/core/idl.service'; import {PcrudService} from '@eg/core/pcrud.service'; export interface ComboboxEntry { @@ -103,6 +104,7 @@ export class ComboboxComponent implements OnInit { constructor( private elm: ElementRef, private store: StoreService, + private idl: IdlService, private pcrud: PcrudService, ) { this.entrylist = []; @@ -119,14 +121,25 @@ export class ComboboxComponent implements OnInit { ngOnInit() { if (this.idlClass) { + const classDef = this.idl.classes[this.idlClass]; + const pkeyField = classDef.pkey; + + if (!pkeyField) { + throw new Error(`IDL class ${this.idlClass} has no pkey field`); + } + + if (!this.idlField) { + this.idlField = classDef.field_map[classDef.pkey].selector || 'name'; + } + this.asyncDataSource = term => { - const field = this.idlField || 'name'; + const field = this.idlField; const args = {}; const extra_args = { order_by : {} }; - args[field] = { 'ilike': `%${term}%`}; // could -or search on label - extra_args['order_by'][this.idlClass] = this.idlField || 'name'; + args[field] = {'ilike': `%${term}%`}; // could -or search on label + extra_args['order_by'][this.idlClass] = field; return this.pcrud.search(this.idlClass, args, extra_args).pipe(map(data => { - return {id: data.id(), label: data[field]()}; + return {id: data[pkeyField](), label: data[field]()}; })); }; } diff --git a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html index 1b36ec8f60..4d3f795c1b 100644 --- a/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html +++ b/Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html @@ -93,6 +93,10 @@ +
+ + +
-- 2.43.2