diff --git a/resources/views/form/rapport-team2/1790853173-2ef535a8-b66e-4889-b29d-db2666f4163a.blade.php b/resources/views/form/rapport-team2/1790853173-2ef535a8-b66e-4889-b29d-db2666f4163a.blade.php index e02a1b14..73ffe3a7 100644 --- a/resources/views/form/rapport-team2/1790853173-2ef535a8-b66e-4889-b29d-db2666f4163a.blade.php +++ b/resources/views/form/rapport-team2/1790853173-2ef535a8-b66e-4889-b29d-db2666f4163a.blade.php @@ -237,7 +237,7 @@ // Loop through contacts and append options to select elements whose id starts with 'field-contact_id' // We assume contact_id fields may be suffixed, e.g. field-contact_id_1, field-contact_id, etc. function applySelect2ToDoctorDropdowns() { - $("[id^='field-contact_id']").each(function () { + $("select[id^='field-contact_id']").each(function () { var select = $(this); // Avoid re-initializing select2 if (select.data("select2")) return; @@ -249,7 +249,7 @@ }); } - function enforceUniqueDoctorSelections() { + /* function enforceUniqueDoctorSelections() { var selectedValues = []; // Collect selected values (ignore empty) @@ -279,9 +279,40 @@ currentSelect.trigger("change.select2"); } }); - } + } */ - $("[id^='field-contact_id']").each(function() { + + function enforceUniqueDoctorSelections() { + var selectedValues = []; + + // Collect selected values from select dropdowns only + $("select[id^='field-contact_id']").each(function () { + var v = $(this).val(); + if (v) selectedValues.push(String(v)); + }); + + // Reset all option disabled states, then disable duplicates across selects + $("select[id^='field-contact_id']").each(function () { + var currentSelect = $(this); + var currentVal = currentSelect.val() ? String(currentSelect.val()) : ""; + + currentSelect.find("option").prop("disabled", false); + + selectedValues.forEach(function (v) { + if (v && v !== currentVal) { + currentSelect + .find("option[value=\"" + v.replace(/"/g, '\\"') + "\"]") + .prop("disabled", true); + } + }); + + if (currentSelect.data("select2")) { + currentSelect.trigger("change.select2"); + } + }); +} + + $("select[id^='field-contact_id']").each(function() { var select = $(this); // Clear previous options select.html('');