LP1831785 Combobox pcrud selector and pkey support
authorBill Erickson <berickxx@gmail.com>
Mon, 29 Jul 2019 15:36:38 +0000 (11:36 -0400)
committerJane Sandberg <sandbej@linnbenton.edu>
Tue, 30 Jul 2019 14:58:58 +0000 (07:58 -0700)
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 <berickxx@gmail.com>
Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu>
Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html

index 1fe37b1..9920632 100644 (file)
@@ -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]()};
                 }));
             };
         }
index 1b36ec8..4d3f795 100644 (file)
     </eg-combobox>
   </div>
   <div class="col-lg-3">
+    <eg-combobox placeholder="Combobox with @idlClass = 'cvrfm'" idlClass="cvrfm" [asyncSupportsEmptyTermClick]="true">
+    </eg-combobox>
+  </div>
+  <div class="col-lg-3">
     <eg-combobox placeholder="Combobox with @idlClass = 'csp'" idlClass="csp" [asyncSupportsEmptyTermClick]="true">
     </eg-combobox>
   </div>