diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 76f0dba2dd..a4a1d8e502 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -423,6 +423,9 @@ function constructFormBody(fields, options) { // Attach edit callbacks (if required) addFieldCallbacks(fields, options); + // Attach clear callbacks (if required) + addClearCallbacks(fields, options); + attachToggle(modal); $(modal + ' .select2-container').addClass('select-full-width'); @@ -571,22 +574,31 @@ function updateFieldValues(fields, options) { if (value == null) { continue; } - var el = $(options.modal).find(`#id_${name}`); + updateFieldValue(name, value, field, options); + } +} - switch (field.type) { - case 'boolean': - el.prop('checked', value); - break; - case 'related field': - // TODO? - break; - case 'file upload': - case 'image upload': - break; - default: - el.val(value); - break; - } + +function updateFieldValue(name, value, field, options) { + var el = $(options.modal).find(`#id_${name}`); + + switch (field.type) { + case 'boolean': + el.prop('checked', value); + break; + case 'related field': + // Clear? + if (value == null && !field.required) { + el.val(null).trigger('change'); + } + // TODO - Specify an actual value! + break; + case 'file upload': + case 'image upload': + break; + default: + el.val(value); + break; } } @@ -777,6 +789,29 @@ function addFieldCallback(name, field, options) { } +function addClearCallbacks(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.required) continue; + + addClearCallback(name, field, options); + } +} + + +function addClearCallback(name, field, options) { + + $(options.modal).find(`#clear_${name}`).click(function() { + updateFieldValue(name, null, field, options); + }); +} + + function initializeRelatedFields(fields, options) { var field_names = options.field_names; @@ -1114,14 +1149,46 @@ function constructField(name, parameters, options) { html += constructLabel(name, parameters); html += `