From 9f13f17e363d9ad10fe88dac69d488f6f9f34877 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 12 Mar 2023 20:43:13 +0100 Subject: [PATCH] Close #18253 by manual merge --- htdocs/core/class/html.form.class.php | 2 ++ htdocs/core/lib/ajax.lib.php | 44 ++++++++++++++++++++------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 8506ab1f352..02aca3cd57b 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1346,6 +1346,8 @@ class Form $out .= img_picto($langs->trans("Search"), 'search'); } + $out .= ajax_event($htmlname, $events); + $out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/societe/ajax/company.php', $urloption, $conf->global->COMPANY_USE_SEARCH_TO_SELECT, 0, $ajaxoptions); } else { // Immediate load of all database diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index cacd22269ad..fde2a988cf7 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -526,24 +526,46 @@ function ajax_combobox($htmlname, $events = array(), $minLengthToAutocomplete = } $msg .= ';'."\n"; - if (is_array($events) && count($events)) { // If an array of js events to do were provided. - $msg .= ' + $msg .= '});'."\n"; + $msg .= "\n"; + + $msg .= ajax_event($htmlname, $events); + + return $msg; +} + + +/** + * Add event management script. + * + * @param string $htmlname Name of html select field ('myid' or '.myclass') + * @param array $events Add some Ajax events option on change of $htmlname component to call ajax to autofill a HTML element (select#htmlname and #inputautocompletehtmlname) + * Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled'))) + * @return string Return JS string to manage event + */ +function ajax_event($htmlname, $events) +{ + $out = ''; + + if (is_array($events) && count($events)) { // If an array of js events to do were provided. + $out = ' + '; } - $msg .= '});'."\n"; - $msg .= "\n"; - - return $msg; + return $out; } + /** * On/off button for constant *