mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Adds generic javascript function for mapping file columns to model fields
This commit is contained in:
parent
f399f4fa34
commit
8f6312f7f4
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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 += `<tr><td><em>${field_name}</em></td><td>${choice_input}</td></tr>`;
|
||||
}
|
||||
|
||||
var headers = `<tr><th>{% trans "File Column" %}</th><th>{% trans "Field Name" %}</th></tr>`;
|
||||
|
||||
var html = '';
|
||||
|
||||
if (options.preamble) {
|
||||
html += options.preamble;
|
||||
}
|
||||
|
||||
html += `<table class='table table-condensed'>${headers}${rows}</table>`;
|
||||
|
||||
constructForm(url, {
|
||||
method: 'POST',
|
||||
title: '{% trans "Select Columns" %}',
|
||||
fields: {},
|
||||
preFormContent: html,
|
||||
onSubmit: function(fields, opts) {
|
||||
|
||||
var columns = [];
|
||||
|
||||
for (var idx = 0; idx < field_names.length; idx++) {
|
||||
columns.push(getFormFieldValue(`column_${idx}`, {}, opts));
|
||||
}
|
||||
|
||||
$(opts.modal).find('#modal-progress-spinner').show();
|
||||
|
||||
inventreePut(
|
||||
opts.url,
|
||||
{
|
||||
columns: columns,
|
||||
rows: data.rows,
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
success: function(response) {
|
||||
handleFormSuccess(response, opts);
|
||||
|
||||
if (options.success) {
|
||||
options.success(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
},
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user