From 9f3f07aff389a25145d9b22ceac6b628cd88bb3d Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 24 Jun 2021 00:06:27 +1000 Subject: [PATCH] Refactor toot-toot - Now can specify the "order" of fields --- InvenTree/templates/js/forms.js | 40 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 1e01de0cc3..6600397ba3 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -193,23 +193,39 @@ function constructCreateForm(url, fields, options={}) { ignored_fields.push('id'); } - for (const key in fields) { - - // Skip over fields - if (allowed_fields && !allowed_fields.includes(key)) { - continue; - } + // Construct an ordered list of field names + var field_names = []; - if (ignored_fields && ignored_fields.includes(key)) { - continue; - } + if (allowed_fields) { + allowed_fields.forEach(function(name) { - var field = fields[key]; + // Only push names which are actually in the set of fields + if (name in fields) { + + if (!ignored_fields.includes(name) && !field_names.includes(name)) { + field_names.push(name); + } + } else { + console.log(`WARNING: '${name}' does not match a valid field name.`); + } + }); + } else { + for (const name in fields) { + + if (!ignored_fields.includes(name) && !field_names.includes(name)) { + field_names.push(name); + } + } + } + + field_names.forEach(function(name) { + + var field = fields[name]; - var f = constructField(key, field, options); + var f = constructField(name, field, options); html += f; - } + }); var modal = '#modal-form';