From: Galen Charlton Date: Tue, 20 Apr 2021 21:57:08 +0000 (-0400) Subject: LP#1904244: fm-editor: add a linkedSearchConditions field option X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=2b02402b2f5bb0deb0394d75df0c99f2ff22fb24;p=evergreen%2Ftadl.git LP#1904244: fm-editor: add a linkedSearchConditions field option This option allows the caller to specify conditions to add when retrieving values for a linked field, e.g., to specify that only active values should be fetched. Example usage: [fieldOptions]="{fund:{linkedSearchConditions:{'active':'t'}}}" If the underlying record has a value set for the linked field, that value will be included when preloadLinkedValues and the value doesn't otherwise meet the search conditions. This patch also sorts the values if preloadLinkedValues is used. Signed-off-by: Galen Charlton Signed-off-by: Ruth Frasur Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts index 9d4ae667f0..fd59734693 100644 --- a/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts +++ b/Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts @@ -56,6 +56,10 @@ export interface FmFieldOptions { // so the user can click or type to find values. preloadLinkedValues?: boolean; + // Additional search conditions to include when constructing + // the query for a linked field's combobox + linkedSearchConditions?: {[field: string]: string}; + // Directly override the required state of the field. // This only has an affect if the value is true. isRequired?: boolean; @@ -584,7 +588,35 @@ export class FmRecordEditorComponent } if (fieldOptions.preloadLinkedValues || !selector) { - return this.pcrud.retrieveAll(field.class, {}, {atomic : true}) + const search = {}; + const orderBy = {order_by: {}}; + if (selector) { + orderBy.order_by[field.class] = selector; + } + const idField = this.idl.classes[field.class].pkey || 'id'; + search[idField] = {'!=' : null}; + if (fieldOptions.linkedSearchConditions) { + const conditions = {}; + Object.keys(fieldOptions.linkedSearchConditions).forEach(key => { + conditions[key] = fieldOptions.linkedSearchConditions[key]; + }); + // ensure that the current value, if present, is included + // in case it doesn't otherwise meet the conditions + const linkedValue = this.record[field.name](); + if (linkedValue !== null && linkedValue !== undefined) { + search['-or'] = []; + const retrieveRec = {}; + retrieveRec[idField] = linkedValue; + search['-or'].push(retrieveRec); + search['-or'].push(conditions); + } else { + // just tack on the conditions + Object.keys(conditions).forEach(key => { + search[key] = conditions[key]; + }); + } + } + return this.pcrud.search(field.class, search, orderBy, {atomic : true}) .toPromise().then(list => { field.linkedValues = this.flattenLinkedValues(field, list); @@ -600,6 +632,11 @@ export class FmRecordEditorComponent const idField = this.idl.classes[field.class].pkey || 'id'; search[selector] = {'ilike': `%${term}%`}; + if (fieldOptions.linkedSearchConditions) { + Object.keys(fieldOptions.linkedSearchConditions).forEach(key => { + search[key] = fieldOptions.linkedSearchConditions[key]; + }); + } orderBy.order_by[field.class] = selector; return this.pcrud.search(field.class, search, orderBy) 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 647cb0cf41..a693944ed2 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 @@ -287,7 +287,8 @@ - +