From 746a0210fea816d1b7a3e1024b80b74c318a085c Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 2 Jul 2021 19:40:02 +1000 Subject: [PATCH] Add option to focus on a particular field when launching --- InvenTree/templates/js/forms.js | 3 +++ InvenTree/templates/js/modals.js | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index c9f725ea19..9f57edd6e5 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -227,6 +227,8 @@ function constructDeleteForm(fields, options) { * options: * - method: The HTTP method e.g. 'PUT', 'POST', 'DELETE' (default='PATCH') * - title: The form title + * - submitText: Text for the "submit" button + * - closeText: Text for the "close" button * - fields: list of fields to display, with the following options * - filters: API query filters * - onEdit: callback when field is edited @@ -237,6 +239,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 + * - 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 * - follow: If a 'url' is provided by the API on success, redirect to it diff --git a/InvenTree/templates/js/modals.js b/InvenTree/templates/js/modals.js index 66f31a7f42..d0f9f742f8 100644 --- a/InvenTree/templates/js/modals.js +++ b/InvenTree/templates/js/modals.js @@ -3,6 +3,12 @@ /* * Create and display a new modal dialog + * + * options: + * - title: Form title to render + * - submitText: Text to render on 'submit' button (default = "Submit") + * - closeText: Text to render on 'close' button (default = "Cancel") + * - focus: Name of field to focus on after launching */ function createNewModal(options={}) { @@ -11,8 +17,8 @@ function createNewModal(options={}) { // Check out what modal forms are already being displayed $('.inventree-modal').each(function() { + var split = this.id.split('-'); - console.log('existing form:', split); var modal_id = parseInt(split[2]); if (modal_id >= id) { @@ -64,6 +70,10 @@ function createNewModal(options={}) { $(modal_name).on('shown.bs.modal', function() { $(modal_name + ' .modal-form-content').scrollTop(0); + + if (options.focus) { + getFieldByName(modal_name, options.focus).focus(); + } }); // Automatically remove the modal when it is deleted!