2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 21:38:48 +00:00

Render simple choice fields with select2

This commit is contained in:
Oliver 2021-06-30 23:18:50 +10:00
parent 770cd9a12d
commit 54731746d8

View File

@ -2,21 +2,23 @@
{% load inventree_extras %} {% load inventree_extras %}
/** /**
*
* This file contains code for rendering (and managing) HTML forms * This file contains code for rendering (and managing) HTML forms
* which are served via the django-drf API. * which are served via the django-drf API.
* *
* The django DRF library provides an OPTIONS method for each API endpoint, * The django DRF library provides an OPTIONS method for each API endpoint,
* which allows us to introspect the available fields at any given endpoint. * which allows us to introspect the available fields at any given endpoint.
* *
* The OPTIONS method provides the following information for each available field: * The OPTIONS method provides the following information for each available field:
* *
* - Field name * - Field name
* - Field label (translated) * - Field label (translated)
* - Field help text (translated) * - Field help text (translated)
* - Field type * - Field type
* - Read / write status * - Read / write status
* - Field required status * - Field required status
* - min_value / max_value * - min_value / max_value
*
*/ */
/* /*
@ -785,17 +787,16 @@ function initializeRelatedFields(fields, options) {
var field = fields[name] || null; var field = fields[name] || null;
if (!field || field.type != 'related field') continue; if (!field || field.hidden) continue;
if (field.hidden) continue; switch (field.type) {
case 'related field':
if (!field.api_url) { initializeRelatedField(name, field, options);
// TODO: Provide manual api_url option? break;
console.log(`Related field '${name}' missing 'api_url' parameter.`); case 'choice':
continue; initializeChoiceField(name, field, options);
break;
} }
initializeRelatedField(name, field, options);
} }
} }
@ -834,6 +835,12 @@ function addSecondaryModal(name, field, options) {
*/ */
function initializeRelatedField(name, field, options) { function initializeRelatedField(name, field, options) {
if (!field.api_url) {
// TODO: Provide manual api_url option?
console.log(`Related field '${name}' missing 'api_url' parameter.`);
return;
}
// Find the select element and attach a select2 to it // Find the select element and attach a select2 to it
var select = $(options.modal).find(`#id_${name}`); var select = $(options.modal).find(`#id_${name}`);
@ -995,6 +1002,17 @@ function initializeRelatedField(name, field, options) {
} }
function initializeChoiceField(name, field, options) {
var select = $(options.modal).find(`#id_${name}`);
select.select2({
dropdownAutoWidth: false,
dropdownParent: $(options.modal),
});
}
// Render a 'no results' element // Render a 'no results' element
function searching() { function searching() {
return `<span>{% trans "Searching" %}...</span>`; return `<span>{% trans "Searching" %}...</span>`;
@ -1343,8 +1361,6 @@ function constructChoiceInput(name, parameters, options) {
var choices = parameters.choices || []; var choices = parameters.choices || [];
// TODO: Select the selected value!
for (var idx = 0; idx < choices.length; idx++) { for (var idx = 0; idx < choices.length; idx++) {
var choice = choices[idx]; var choice = choices[idx];