diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 4f15281427..1faab4f9ab 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -881,16 +881,51 @@ function initializeRelatedFields(fields, options) { */ function addSecondaryModal(name, field, options) { + var secondary = field.secondary; + var html = ` -
- ${field.secondary.label} +
+ ${secondary.label || secondary.title}
`; $(options.modal).find(`label[for="id_${name}"]`).append(html); // TODO: Launch a callback + $(options.modal).find(`#btn-new-${name}`).click(function() { + + if (secondary.callback) { + // A "custom" callback can be specified for the button + secondary.callback(field, options); + } else if (secondary.api_url) { + // By default, a new modal form is created, with the parameters specified + // The parameters match the "normal" form creation parameters + + secondary.onSuccess = function(data, opts) { + + // Add a new "option" to the existing field + // TODO: Refactor this + + var select = $(options.modal).find(`#id_${name}`); + + var option = new Option(name, data.pk, true, true); + + option.instance = data; + + select.append(option).trigger('change'); + + select.trigger({ + type: 'select2:select', + params: { + data: data, + } + }); + }; + + constructForm(secondary.api_url, secondary); + } + }); }