matrici cache

This commit is contained in:
2026-03-31 14:25:10 +03:00
parent d983659000
commit d24836e2b1
5 changed files with 187 additions and 111 deletions
+73 -110
View File
@@ -344,7 +344,7 @@ $(document).ready(function () {
if (iddatadb) {
if (matrici.length === 0) {
$.ajax({
url: "get_matrici_db.php",
url: "get_matrici.php",
method: "GET",
dataType: "json",
success: function (data) {
@@ -1615,18 +1615,6 @@ $(document).ready(function () {
return;
}
// Filtra le matrici in base alla MacroMatrice selezionata
const filteredMatrici = selectedMacro
? matrici.filter(
(matrice) => matrice.MacroMatrice === selectedMacro,
)
: matrici;
// Crea opzioni per Select2
const options = filteredMatrici.map(function (matrice) {
return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
});
// Memorizza il valore corrente
const currentValue = $select.val();
@@ -1635,39 +1623,39 @@ $(document).ready(function () {
$select.select2("destroy");
}
// Inizializza Select2
// Inizializza Select2 con AJAX
$select.empty().select2({
placeholder: filteredMatrici.length
? "Seleziona matrice globale"
: "Nessuna matrice disponibile",
placeholder: "Seleziona matrice globale",
allowClear: true,
data: options,
dropdownParent: $("#partsModal"),
minimumResultsForSearch: 0, // Abilita ricerca senza limite minimo di caratteri
matcher: function (params, data) {
if (!params.term) return data; // Mostra tutte le opzioni se non c'è termine di ricerca
const term = params.term.toUpperCase();
if (data.text.toUpperCase().indexOf(term) >= 0) return data;
return null;
minimumInputLength: 0,
ajax: {
url: "search_matrici.php",
dataType: "json",
delay: 150,
data: function (params) {
return { q: params.term || "", limit: 20, macro: selectedMacro || "" };
},
processResults: function (data) {
return { results: data.results || [] };
},
cache: true,
},
});
// Ripristina il valore corrente se valido
if (
currentValue &&
filteredMatrici.some((m) => m.IdMatrice == currentValue)
) {
$select.val(currentValue).trigger("change");
} else if (filteredMatrici.length === 0) {
const errorMsg = $(
'<div class="alert alert-warning temp-alert" role="alert">Nessuna matrice disponibile per la MacroMatrice selezionata.</div>',
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
// Ripristina il valore corrente
if (currentValue) {
$.ajax({
url: "search_matrici.php",
data: { id: currentValue },
dataType: "json",
}).then(function (data) {
const item = (data.results || [])[0];
if (item) {
const option = new Option(item.text, item.id, true, true);
$select.append(option).trigger("change");
}
});
}
}
@@ -1686,32 +1674,6 @@ $(document).ready(function () {
return;
}
// Filtra le matrici in base alla MacroMatrice selezionata
const filteredMatrici = selectedMacro
? matrici.filter((m) => m.MacroMatrice === selectedMacro)
: matrici;
// Crea opzioni per Select2, includendo il valore pre-selezionato se non è nel filtro
let options = filteredMatrici.map(function (matrice) {
return { id: matrice.IdMatrice, text: matrice.NomeMatrice };
});
// Se idmatrice esiste ed è fuori dal filtro, aggiungilo come opzione
if (
idmatrice &&
!filteredMatrici.some((m) => m.IdMatrice == idmatrice)
) {
const selectedMatrice = matrici.find(
(m) => m.IdMatrice == idmatrice,
);
if (selectedMatrice) {
options.push({
id: selectedMatrice.IdMatrice,
text: selectedMatrice.NomeMatrice,
});
}
}
// Memorizza il valore corrente
const currentValue = idmatrice || $select.val();
@@ -1720,44 +1682,44 @@ $(document).ready(function () {
$select.select2("destroy");
}
// Inizializza Select2
// Inizializza Select2 con AJAX
$select.empty().select2({
placeholder: filteredMatrici.length
? "Seleziona matrice"
: "Nessuna matrice disponibile",
placeholder: "Seleziona matrice",
allowClear: true,
data: options,
dropdownParent: $("#partsModal"),
minimumResultsForSearch: 0, // Abilita ricerca senza limite minimo di caratteri
matcher: function (params, data) {
if (!params.term) return data; // Mostra tutte le opzioni se non c'è termine di ricerca
const term = params.term.toUpperCase();
if (data.text.toUpperCase().indexOf(term) >= 0) return data;
return null;
minimumInputLength: 0,
ajax: {
url: "search_matrici.php",
dataType: "json",
delay: 150,
data: function (params) {
return { q: params.term || "", limit: 20, macro: selectedMacro || "" };
},
processResults: function (data) {
return { results: data.results || [] };
},
cache: true,
},
});
// Ripristina il valore se valido
if (partId && partId !== "new" && currentValue) {
const matrice = matrici.find((m) => m.IdMatrice == currentValue);
if (matrice) {
const option = new Option(
matrice.NomeMatrice,
matrice.IdMatrice,
true,
true,
);
if (!fromFilter) $select.append(option).trigger("change");
else $select.append(option);
partMatrice[partNumber] = matrice.IdMatrice;
} else {
// Aggiusta valore non valido
if (!fromFilter) $select.val(null).trigger("change");
partMatrice[partNumber] = null;
}
$.ajax({
url: "search_matrici.php",
data: { id: currentValue },
dataType: "json",
}).then(function (data) {
const item = (data.results || [])[0];
if (item) {
const option = new Option(item.text, item.id, true, true);
if (!fromFilter) $select.append(option).trigger("change");
else $select.append(option);
partMatrice[partNumber] = item.id;
} else {
if (!fromFilter) $select.val(null).trigger("change");
partMatrice[partNumber] = null;
}
});
} else {
$select.val(null).trigger("change", [{ skipHandler: true }]);
}
@@ -1834,18 +1796,7 @@ $(document).ready(function () {
}
});
// Mostra messaggio se non ci sono matrici
if (filteredMatrici.length === 0 && selectedMacro) {
const errorMsg = $(
'<div class="alert alert-warning temp-alert" role="alert">Nessuna matrice disponibile per la MacroMatrice selezionata.</div>',
);
$("#partsModal .modal-body").prepend(errorMsg);
setTimeout(function () {
errorMsg.fadeOut(500, function () {
$(this).remove();
});
}, 5000);
}
// Messaggio se macro selezionata ma nessun risultato sarà gestito dal placeholder Select2
// Debug per verificare inizializzazione
console.log(
@@ -1861,8 +1812,13 @@ $(document).ready(function () {
$(document).on("click", ".propagate-matrice-btn", function () {
const $row = $(this).closest("tr");
const globalVal = $("#global-matrice").val();
const globalText = $("#global-matrice").find("option:selected").text();
if (globalVal) {
$row.find(".part-matrice").val(globalVal).trigger("change");
const $target = $row.find(".part-matrice");
if (!$target.find(`option[value="${globalVal}"]`).length) {
$target.append(new Option(globalText, globalVal, true, true));
}
$target.val(globalVal).trigger("change");
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Seleziona una matrice globale prima di propagare.</div>',
@@ -1878,8 +1834,15 @@ $(document).ready(function () {
$(document).on("click", ".propagate-all-btn", function () {
const globalVal = $("#global-matrice").val();
const globalText = $("#global-matrice").find("option:selected").text();
if (globalVal) {
$("#partsTableBody .part-matrice").val(globalVal).trigger("change");
$("#partsTableBody .part-matrice").each(function () {
const $target = $(this);
if (!$target.find(`option[value="${globalVal}"]`).length) {
$target.append(new Option(globalText, globalVal, true, true));
}
$target.val(globalVal).trigger("change");
});
} else {
const errorMsg = $(
'<div class="alert alert-danger temp-alert" role="alert">Seleziona una matrice globale prima di propagare.</div>',