diff --git a/InvenTree/part/templates/part/upload_bom.html b/InvenTree/part/templates/part/upload_bom.html index bbb69e3083..1646a9b70c 100644 --- a/InvenTree/part/templates/part/upload_bom.html +++ b/InvenTree/part/templates/part/upload_bom.html @@ -88,96 +88,24 @@ $('#bom-upload').click(function() { // Clear existing entries from the table $('.bom-import-row').remove(); - // Disable the "submit" button - $('#bom-submit').show(); + selectImportFields( + '{% url "api-bom-import-extract" %}', + response, + { + success: function(response) { + constructBomUploadTable(response); - var fields = {}; + // Show the "submit" button + $('#bom-submit').show(); - var choices = []; - - // Add an "empty" value - choices.push({ - value: '', - display_name: '-----', - }); - - for (const [name, field] of Object.entries(response.model_fields)) { - choices.push({ - value: name, - display_name: field.label || name, - }); - } - - var field_names = Object.keys(response.file_fields); - - for (var idx = 0; idx < field_names.length; idx++) { - - var field_name = field_names[idx]; - - // Construct a new field - fields[`column_${idx}`] = { - type: 'choice', - label: field_name, - value: response.file_fields[field_name].value, - choices: choices, - inline: true, - }; - } - - constructForm('{% url "api-bom-import-extract" %}', { - method: 'POST', - title: '{% trans "Select BOM Columns" %}', - fields: fields, - onSubmit: function(fields, opts) { - var columns = []; - - for (var idx = 0; idx < field_names.length; idx++) { - columns.push( - getFormFieldValue(`column_${idx}`, {}, {}) - ); + $('#bom-submit').click(function() { + submitBomTable({{ part.pk }}, { + bom_data: response, + }); + }); } - - $(opts.modal).find('#modal-progress-spinner').show(); - - inventreePut( - opts.url, - { - columns: columns, - rows: response.rows, - }, - { - method: 'POST', - success: function(r) { - handleFormSuccess(r, opts); - - constructBomUploadTable(r); - - $('#bom-submit').click(function() { - submitBomTable({{ part.pk }}, { - bom_data: response, - }); - }); - }, - error: function(xhr) { - - $(opts.modal).find('#modal-progress-spinner').hide(); - - switch (xhr.status) { - case 400: - handleFormErrors(xhr.responseJSON, fields, opts); - break; - default: - $(opts.modal).modal('hide'); - - console.log(`upload error at ${opts.url}`); - showApiError(xhr, opts.url); - break; - } - } - } - ); - }, - }); + } + ); } }); diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index b8f387328b..6ab183cc96 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -31,6 +31,7 @@ setFormInputPlaceholder, setFormGroupVisibility, showFormInput, + selectImportFields, */ /** @@ -895,8 +896,8 @@ function getFormFieldValue(name, field={}, options={}) { // Find the HTML element var el = getFormFieldElement(name, options); - if (!el) { - console.log(`ERROR: getFormFieldValue could not locate field '{name}'`); + if (!el.exists()) { + console.log(`ERROR: getFormFieldValue could not locate field '${name}'`); return null; } @@ -2080,7 +2081,7 @@ function constructLabel(name, parameters) { * - parameters: Field parameters returned by the OPTIONS method * */ -function constructInput(name, parameters, options) { +function constructInput(name, parameters, options={}) { var html = ''; @@ -2422,3 +2423,117 @@ function constructHelpText(name, parameters) { return html; } + + +/* + * Construct a dialog to select import fields + */ +function selectImportFields(url, data={}, options={}) { + + if (!data.model_fields) { + console.log("WARNING: selectImportFields is missing 'model_fields'"); + return; + } + + if (!data.file_fields) { + console.log("WARNING: selectImportFields is missing 'file_fields'"); + return; + } + + var choices = []; + + // Add an "empty" value + choices.push({ + value: '', + display_name: '-----', + }); + + for (const [name, field] of Object.entries(data.model_fields)) { + choices.push({ + value: name, + display_name: field.label || name, + }); + } + + var rows = ''; + + var field_names = Object.keys(data.file_fields); + + for (var idx = 0; idx < field_names.length; idx++) { + + var field_name = field_names[idx]; + + var choice_input = constructInput( + `column_${idx}`, + { + type: 'choice', + label: field_name, + value: data.file_fields[field_name].value, + choices: choices, + } + ); + + rows += `