diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index a31da2529d..8e2d3a4c66 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -974,10 +974,10 @@ function initializeRelatedFields(fields, options) { switch (field.type) { case 'related field': - initializeRelatedField(name, field, options); + initializeRelatedField(field, fields, options); break; case 'choice': - initializeChoiceField(name, field, options); + initializeChoiceField(field, fields, options); break; } } @@ -992,7 +992,9 @@ function initializeRelatedFields(fields, options) { * - field: The field data object * - options: The options object provided by the client */ -function addSecondaryModal(name, field, options) { +function addSecondaryModal(field, fields, options) { + + var name = field.name; var secondary = field.secondary; @@ -1005,22 +1007,41 @@ function addSecondaryModal(name, field, options) { $(options.modal).find(`label[for="id_${name}"]`).append(html); - // TODO: Launch a callback + // Callback function when the secondary button is pressed $(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 + // Determine the API query URL + var url = secondary.api_url || field.api_url; - secondary.onSuccess = function(data, opts) { - setRelatedFieldData(name, data, options); - }; + // Extract form values at time of button press + var data = extractFormData(fields, options) - constructForm(secondary.api_url, secondary); + // Allow the secondary form to be "prefilled" with a custom function + if (secondary.prefill) { + secondary.fields = secondary.prefill(data); } + + // If no onSuccess function is defined, provide a default one + if (!secondary.onSuccess) { + secondary.onSuccess = function(data, opts) { + + // Force refresh from the API, to get full detail + inventreeGet(`${url}${data.pk}/`, {}, { + success: function(responseData) { + + setRelatedFieldData(name, responseData, options); + } + }); + }; + } + + // Method should be "POST" for creation + secondary.method = secondary.method || 'POST'; + + constructForm( + url, + secondary + ); }); } @@ -1034,7 +1055,9 @@ function addSecondaryModal(name, field, options) { * - field: Field definition from the OPTIONS request * - options: Original options object provided by the client */ -function initializeRelatedField(name, field, options) { +function initializeRelatedField(field, fields, options) { + + var name = field.name; if (!field.api_url) { // TODO: Provide manual api_url option? @@ -1047,7 +1070,7 @@ function initializeRelatedField(name, field, options) { // Add a button to launch a 'secondary' modal if (field.secondary != null) { - addSecondaryModal(name, field, options); + addSecondaryModal(field, fields, options); } // TODO: Add 'placeholder' support for entry select2 fields