diff --git a/assets/libs/initTomSelect.js b/assets/libs/initTomSelect.js index bee27cb..1e792c5 100644 --- a/assets/libs/initTomSelect.js +++ b/assets/libs/initTomSelect.js @@ -80,6 +80,63 @@ export function initTomSelect(parent = document) { }); } } + else if (el.getAttribute('data-load') === "options") { + const setupSelect = (data) => { + new TomSelect(el, { + valueField: 'id', + labelField: 'name', + searchField: 'name', + options: data, + maxOptions: null, + // LORSQU'ON SÉLECTIONNE UN PRODUIT + // Dans admin.js, section onChange de TomSelect : + onChange: (id) => { + if (!id) return; + + // On s'assure de trouver le produit (id peut être string ou int) + const product = data.find(p => String(p.id) === String(id)); + const row = el.closest('.form-repeater__row') || el.closest('fieldset'); + if (!row) return; + const priceInput = row.querySelector('input[name*="[price_ht]"]'); + + if (priceInput) { + priceInput.value = product.price; + // Indispensable pour que d'autres scripts (calcul totaux) voient le changement + priceInput.dispatchEvent(new Event('input', { bubbles: true })); + priceInput.dispatchEvent(new Event('change', { bubbles: true })); + } + + }, + render: { + option: (data, escape) => ` +