From e58507977995fc4f324445ac86c53f9dcb3eacc2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Jun 2021 19:32:48 +1000 Subject: [PATCH] Callback function for fields after editing --- InvenTree/templates/js/forms.js | 37 ++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 833c4eb667..9c24aa7780 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -272,6 +272,9 @@ function constructFormBody(fields, options) { // Secondary modal options fields[field].secondary = field_options.secondary; + + // Edit callback + fields[field].onEdit = field_options.onEdit; } } @@ -335,7 +338,10 @@ function constructFormBody(fields, options) { updateFieldValues(fields, options); // Setup related fields - initializeRelatedFields(fields, options) + initializeRelatedFields(fields, options); + + // Attach edit callbacks (if required) + addFieldCallbacks(fields, options); attachToggle(modal); @@ -548,6 +554,35 @@ function handleFormErrors(errors, fields, options) { } +/* + * Attach callbacks to specified fields, + * triggered after the field value is edited. + * + * Callback function is called with arguments (name, field, options) + */ +function addFieldCallbacks(fields, options) { + + for (var idx = 0; idx < options.field_names.length; idx++) { + + var name = options.field_names[idx]; + + var field = fields[name]; + + if (!field || !field.onEdit) continue; + + addFieldCallback(name, field, options); + } +} + + +function addFieldCallback(name, field, options) { + + $(options.modal).find(`#id_${name}`).change(function() { + field.onEdit(name, field, options); + }); +} + + function initializeRelatedFields(fields, options) { var field_names = options.field_names;