From 408ff639ddb18e6c0d539aed7754f759dcea1bb3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 4 Aug 2021 23:48:21 +1000 Subject: [PATCH] Adds ability to pre-fill a form with a complete dataset --- InvenTree/templates/js/translated/forms.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 27337d97e7..3b55802f38 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -240,6 +240,7 @@ function constructDeleteForm(fields, options) { * - hidden: Set to true to hide the field * - icon: font-awesome icon to display before the field * - prefix: Custom HTML prefix to display before the field + * - data: map of data to fill out field values with * - focus: Name of field to focus on when modal is displayed * - preventClose: Set to true to prevent form from closing on success * - onSuccess: callback function when form action is successful @@ -263,6 +264,11 @@ function constructForm(url, options) { // Default HTTP method options.method = options.method || 'PATCH'; + // Construct an "empty" data object if not provided + if (!options.data) { + options.data = {}; + } + // Request OPTIONS endpoint from the API getApiEndpointOptions(url, function(OPTIONS) { @@ -346,10 +352,19 @@ function constructFormBody(fields, options) { // otherwise *all* fields will be displayed var displayed_fields = options.fields || fields; + // Handle initial data overrides + if (options.data) { + for (const field in options.data) { + + if (field in fields) { + fields[field].value = options.data[field]; + } + } + } + // Provide each field object with its own name for(field in fields) { fields[field].name = field; - // If any "instance_filters" are defined for the endpoint, copy them across (overwrite) if (fields[field].instance_filters) {