Add auto-fill option to select2 inputs on API forms

- WIll fill input if a single result is returned
This commit is contained in:
Oliver 2021-10-05 00:39:22 +11:00
parent 8a90b9df6d
commit 416ba51e22
4 changed files with 44 additions and 6 deletions

View File

@ -170,7 +170,7 @@
{% if build.active %} {% if build.active %}
<div class='btn-group' role='group'> <div class='btn-group' role='group'>
<button class='btn btn-success' type='button' id='btn-auto-allocate' title='{% trans "Allocate stock to build" %}'> <button class='btn btn-success' type='button' id='btn-auto-allocate' title='{% trans "Allocate stock to build" %}'>
<span class='fas fa-magic'></span> {% trans "Auto Allocate" %} <span class='fas fa-sign-in-alt'></span> {% trans "Allocate Stock" %}
</button> </button>
<button class='btn btn-danger' type='button' id='btn-unallocate' title='{% trans "Unallocate stock" %}'> <button class='btn btn-danger' type='button' id='btn-unallocate' title='{% trans "Unallocate stock" %}'>
<span class='fas fa-minus-circle'></span> {% trans "Unallocate Stock" %} <span class='fas fa-minus-circle'></span> {% trans "Unallocate Stock" %}

View File

@ -383,7 +383,7 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
partId, partId,
{ {
success: function(data) { success: function(data) {
console.log("here we go I guess"); // TODO: Reload table
}, },
parts: [ parts: [
parseInt(pk), parseInt(pk),
@ -959,13 +959,17 @@ function allocateStockToBuild(build_id, part_id, options={}) {
filters: { filters: {
part: bom_item.sub_part, part: bom_item.sub_part,
in_stock: true, in_stock: true,
part_detail: true, part_detail: false,
location_detail: true, location_detail: true,
}, },
model: 'stockitem', model: 'stockitem',
required: true,
render_part_detail: false, render_part_detail: false,
render_location_detail: true, render_location_detail: true,
// TODO: Auto-assign value? auto_fill: true,
noResults: function(query) {
return '{% trans "No matching stock items" %}';
}
}, },
null, null,
options, options,

View File

@ -1280,7 +1280,7 @@ function initializeRelatedField(field, fields, options) {
if (!field.api_url) { if (!field.api_url) {
// TODO: Provide manual api_url option? // TODO: Provide manual api_url option?
console.log(`Related field '${name}' missing 'api_url' parameter.`); console.log(`WARNING: Related field '${name}' missing 'api_url' parameter.`);
return; return;
} }
@ -1301,6 +1301,15 @@ function initializeRelatedField(field, fields, options) {
placeholder: '', placeholder: '',
dropdownParent: $(options.modal), dropdownParent: $(options.modal),
dropdownAutoWidth: false, dropdownAutoWidth: false,
language: {
noResults: function(query) {
if (field.noResults) {
return field.noResults(query);
} else {
return '{% trans "No results found" %}';
}
}
},
ajax: { ajax: {
url: field.api_url, url: field.api_url,
dataType: 'json', dataType: 'json',
@ -1415,8 +1424,10 @@ function initializeRelatedField(field, fields, options) {
} }
}); });
// If a 'value' is already defined, grab the model info from the server // If a 'value' is already defined, grab the model info from the server
if (field.value) { if (field.value) {
var pk = field.value; var pk = field.value;
var url = `${field.api_url}/${pk}/`.replace('//', '/'); var url = `${field.api_url}/${pk}/`.replace('//', '/');
@ -1425,6 +1436,25 @@ function initializeRelatedField(field, fields, options) {
setRelatedFieldData(name, data, options); setRelatedFieldData(name, data, options);
} }
}); });
} else if (field.auto_fill) {
// Attempt to auto-fill the field
var filters = field.filters || {};
// Enforce pagination, limit to a single return (for fast query)
filters.limit = 1;
filters.offset = 0;
inventreeGet(field.api_url, field.filters || {}, {
success: function(data) {
// Only a single result is available, given the provided filters
if (data.count == 1) {
setRelatedFieldData(name, data.results[0], options);
}
}
});
} }
} }

View File

@ -47,7 +47,11 @@ function renderCompany(name, data, parameters, options) {
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
function renderStockItem(name, data, parameters, options) { function renderStockItem(name, data, parameters, options) {
var image = data.part_detail.thumbnail || data.part_detail.image || blankImage(); var image = blankImage();
if (data.part_detail) {
image = data.part_detail.thumbnail || data.part_detail.image || blankImage();
}
var html = ''; var html = '';