added select 2 on all fields (with limit of 12)

This commit is contained in:
Claudio 2026-02-18 10:48:04 +01:00
parent 73dd8f4ce8
commit 4bb0445cff
2 changed files with 66 additions and 28 deletions

File diff suppressed because one or more lines are too long

View File

@ -1818,26 +1818,50 @@ function fixedDefaultValue(array $f): string
return; return;
} }
const items = dropdownData[fieldId];
const valToSelect = currentValue || selectedValue;
if (items.length > 12) {
// Select2 con ricerca
dropdown.innerHTML = '<option value="">Seleziona...</option>'; dropdown.innerHTML = '<option value="">Seleziona...</option>';
dropdownData[fieldId].forEach(value => { items.forEach(value => {
const option = document.createElement('option'); const option = document.createElement('option');
option.value = value.IdCustomFieldsValue; option.value = value.IdCustomFieldsValue;
option.textContent = value.Valore; option.textContent = value.Valore;
if (currentValue && currentValue === String(value.IdCustomFieldsValue)) { if (valToSelect && valToSelect === String(value.IdCustomFieldsValue)) {
option.selected = true;
} else if (!currentValue && selectedValue === String(value.IdCustomFieldsValue)) {
option.selected = true; option.selected = true;
} }
dropdown.appendChild(option); dropdown.appendChild(option);
}); });
$(dropdown).select2({
// Ripristina il valore corrente placeholder: "Seleziona...",
if (currentValue || selectedValue) { allowClear: true,
dropdown.value = currentValue || selectedValue; width: '100%'
const event = new Event('change', {
bubbles: true
}); });
dropdown.dispatchEvent(event); if (valToSelect) {
$(dropdown).val(valToSelect).trigger('change');
}
} else {
// Select nativa
try {
$(dropdown).select2('destroy');
} catch (e) {}
dropdown.innerHTML = '<option value="">Seleziona...</option>';
items.forEach(value => {
const option = document.createElement('option');
option.value = value.IdCustomFieldsValue;
option.textContent = value.Valore;
if (valToSelect && valToSelect === String(value.IdCustomFieldsValue)) {
option.selected = true;
}
dropdown.appendChild(option);
});
if (valToSelect) {
dropdown.value = valToSelect;
dropdown.dispatchEvent(new Event('change', {
bubbles: true
}));
}
} }
}); });
} }
@ -2326,6 +2350,8 @@ function fixedDefaultValue(array $f): string
results = await loadFixedFieldData(fieldKey); results = await loadFixedFieldData(fieldKey);
} }
if (results.length > 12) {
// Select2 con ricerca
$select.select2('destroy').empty().select2({ $select.select2('destroy').empty().select2({
data: [{ data: [{
id: '', id: '',
@ -2335,6 +2361,18 @@ function fixedDefaultValue(array $f): string
allowClear: true, allowClear: true,
width: '100%' width: '100%'
}); });
} else {
// Select nativa senza Select2
try {
$select.select2('destroy');
} catch (e) {}
$select.empty();
$select.append(new Option('Seleziona...', '', true, false));
results.forEach(r => {
$select.append(new Option(r.text, r.id));
});
$select.css('width', '100%');
}
// Imposta valore iniziale // Imposta valore iniziale
if (currentVal) { if (currentVal) {