diff --git a/public/userarea/gridRenderer.js b/public/userarea/gridRenderer.js index d511a4d..44d6ee5 100644 --- a/public/userarea/gridRenderer.js +++ b/public/userarea/gridRenderer.js @@ -134,12 +134,35 @@ ); } + function sortSelect2ResultsByStart(data) { + const term = $(".select2-container--open .select2-search__field").val(); + + if (!term) { + return data; + } + + const search = term.toLowerCase().trim(); + + return data.sort(function (a, b) { + const textA = (a.text || "").toLowerCase().trim(); + const textB = (b.text || "").toLowerCase().trim(); + + const aStarts = textA.startsWith(search); + const bStarts = textB.startsWith(search); + + if (aStarts && !bStarts) return -1; + if (!aStarts && bStarts) return 1; + + return textA.localeCompare(textB, "it", { sensitivity: "base" }); + }); + } // Select2 AJAX config for client selects const clientSelect2Config = { placeholder: "Search client...", allowClear: true, width: "100%", minimumInputLength: 0, + sorter: sortSelect2ResultsByStart, dropdownCssClass: "select2-dropdown-smaller", ajax: { url: "search_clienti.php", @@ -341,6 +364,7 @@ allowClear: true, width: "100%", minimumInputLength: 0, + sorter: sortSelect2ResultsByStart, ajax: { url: "search_customfield_values.php", dataType: "json", @@ -1133,6 +1157,7 @@ placeholder: "Seleziona...", allowClear: true, width: "100%", + sorter: sortSelect2ResultsByStart, }); } else { const items = fixedFieldCache[fieldKey] || [];