From ff6df5be555c3ec4ff1dfdd976aa0d2a66e3d0d9 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 11 Jun 2025 11:14:47 +0100 Subject: [PATCH] Bug 39916: Fix race condition in booking modal edit mode When editing an existing booking, the item type field was not being populated because setFormValues() was triggering the wrong Select2 event. The fix ensures both 'change' and 'select2:select' events are triggered with proper data structure, allowing the item type to be automatically populated based on the selected item's effective_item_type_id. A small timeout prevents race conditions during DOM initialization. --- .../prog/js/modals/place_booking.js | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js index 8b9f2d91b0c..bed0fe8388c 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js +++ b/koha-tmpl/intranet-tmpl/prog/js/modals/place_booking.js @@ -1113,6 +1113,27 @@ function setFormValues( }); } + // If passed an itemnumber, pre-select + if (booking_item_id) { + // Wait a bit for the item options to be fully created with data attributes + setTimeout(function() { + $("#booking_item_id").val(booking_item_id).trigger("change"); + // Also trigger the select2:select event with proper data + let selectedOption = $("#booking_item_id option:selected")[0]; + if (selectedOption) { + $("#booking_item_id").trigger({ + type: "select2:select", + params: { + data: { + id: booking_item_id, + element: selectedOption + } + } + }); + } + }, 100); + } + // Set booking start & end if this is an edit if (start_date) { // Allow invalid pre-load so setDate can set date range @@ -1128,11 +1149,6 @@ function setFormValues( else { periodPicker.redraw(); } - - // If passed an itemnumber, pre-select - if (booking_item_id) { - $("#booking_item_id").val(booking_item_id).trigger("change"); - } } $("#placeBookingForm").on("submit", function (e) { -- 2.49.0