diff --git a/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php b/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php index 75307b21..eb500a6b 100644 --- a/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php +++ b/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php @@ -77,7 +77,7 @@ @break @case('dropdown') - required == 'yes' ? 'required' : '' }}> @foreach ($question->options as $option) @if($question->code == 'contact_id' && isset($option->contact->type) && $option->contact->type == 'doctor' ) @@ -220,7 +220,6 @@ function applySelect2ToDoctorDropdowns() { $("[id^='field-contact_id']").each(function () { var select = $(this); - // Avoid re-initializing select2 if (select.data("select2")) return; select.select2({ width: "100%", @@ -246,7 +245,6 @@ currentSelect.find("option").prop("disabled", false); - // Disable values selected in OTHER selects (keep current selection enabled) selectedValues.forEach(function (v) { if (v && v !== currentVal) { currentSelect @@ -255,84 +253,80 @@ } }); - // Refresh select2 UI if present if (currentSelect.data("select2")) { currentSelect.trigger("change.select2"); } }); } - + $("[id^='field-contact_id']").each(function() { - var select = $(this); - // Clear previous options - var existingVal = select.val(); - select.html(''); - // Add a default 'Select' option - select.append( - $("") - .attr("value", '') - .text('-- Select --') - ); - // Append options from contacts - contacts.forEach(function(itm) { - var option = $("") - .attr("value", itm.value) - .text(itm.display); - // If doctor options have extra attributes - if (itm.contact && itm.contact.msl_code) { - option.attr("msl_code", itm.contact.msl_code); - } - if (itm.contact && itm.contact.speciality) { - option.attr("speciality", itm.contact.speciality); - } - select.append(option); - }); + var select = $(this); - // For each select element, add a change event handler to populate `msl_code` and `speciality` - // We assume dropdowns and targets follow the pattern: - // - select: field-contact_id_1, field-contact_id_2, ..., field-contact_id - // - result: field-msl_code_1, field-speciality_1, etc. + + var existingVal = select.attr('data-initial-value') || select.val(); - // Remove any old event handlers first to prevent duplicate bindings + + select.empty(); - if (existingVal) { - select.val(existingVal); - } + // Default option + select.append($("").attr("value", '').text('-- Select --')); - select.off('change').on('change', function () { - var selected = $(this).find(':selected'); - - var selectId = $(this).attr('id'); // e.g., field-contact_id_1 - // Extract the suffix (e.g., _1 or empty) - var match = selectId.match(/^field-contact_id(_\d+)?$/); - var suffix = match && match[1] ? match[1] : ''; + + contacts.forEach(function(itm) { + var option = $("") + .attr("value", itm.value) + .text(itm.display); - // Build the target msl_code and speciality field ids - var mslCodeField = $('#field-msl_code' + suffix); - var specialityField = $('#field-speciality' + suffix); + if (itm.contact && itm.contact.msl_code) { + option.attr("msl_code", itm.contact.msl_code); + } + if (itm.contact && itm.contact.speciality) { + option.attr("speciality", itm.contact.speciality); + } + select.append(option); + }); - if (mslCodeField.length) { - mslCodeField.val(selected.attr('msl_code') || ''); - } - if (specialityField.length) { - specialityField.val(selected.attr('speciality') || ''); - } + + if (existingVal) { + select.val(String(existingVal)); + } - // Prevent selecting the same doctor across multiple dropdowns - enforceUniqueDoctorSelections(); - }); + + select.off('change').on('change', function () { + var selected = $(this).find(':selected'); + var selectId = $(this).attr('id'); + var match = selectId.match(/^field-contact_id(_\d+)?$/); + var suffix = match && match[1] ? match[1] : ''; - if (existingVal) { - select.trigger('change'); - } + var mslCodeField = $('#field-msl_code' + suffix); + var specialityField = $('#field-speciality' + suffix); - // Ensure unique selection is enforced after initial render - enforceUniqueDoctorSelections(); - }); + if (mslCodeField.length) { + mslCodeField.val(selected.attr('msl_code') || ''); + } + if (specialityField.length) { + specialityField.val(selected.attr('speciality') || ''); + } - // Enable search on doctor dropdowns - applySelect2ToDoctorDropdowns(); - $("#pageloader").fadeOut(); + enforceUniqueDoctorSelections(); + }); + }); + + + applySelect2ToDoctorDropdowns(); + + + $("[id^='field-contact_id']").each(function() { + var select = $(this); + var existingVal = select.attr('data-initial-value'); + if (existingVal) { + select.val(String(existingVal)).trigger('change'); + } + }); + + $("#pageloader").fadeOut(); + + });