Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport FIX manage event in select_company with AJAX search option on #32661

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions htdocs/core/class/html.form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,9 @@ public function select_company($selected = '', $htmlname = 'socid', $filter = ''
if ($hidelabel == 3) {
$out .= img_picto($langs->trans("Search"), 'search');
}

$out .= ajax_event($htmlname, $events);

} else {
// Immediate load of all database
$out .= $this->select_thirdparty_list($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $events, '', 0, $limit, $morecss, $moreparam, $multiple, $excludeids);
Expand Down
42 changes: 32 additions & 10 deletions htdocs/core/lib/ajax.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,17 +476,39 @@ 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 .= "</script>\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 = '<!-- JS code to manage event for id = ' . $htmlname . ' -->
<script>
$(document).ready(function () {
jQuery("#'.$htmlname.'").change(function () {
var obj = '.json_encode($events).';
var obj = '.json_encode($events) . ';
$.each(obj, function(key,values) {
if (values.method.length) {
runJsCodeForEvent'.$htmlname.'(values);
}
});
});

function runJsCodeForEvent'.$htmlname.'(obj) {
var id = $("#'.$htmlname.'").val();
var method = obj.method;
Expand Down Expand Up @@ -517,23 +539,23 @@ function(response) {
var selecthtml_str = response.value;
var selecthtml_dom=$.parseHTML(selecthtml_str);
if (typeof(selecthtml_dom[0][0]) !== \'undefined\') {
$("#inputautocomplete"+htmlname).val(selecthtml_dom[0][0].innerHTML);
$("#inputautocomplete"+htmlname).val(selecthtml_dom[0][0].innerHTML);
}
} else {
$("#inputautocomplete"+htmlname).val("");
}
$("select#" + htmlname).change(); /* Trigger event change */
}
);
}';
}
});
</script>';
}

$msg .= '});'."\n";
$msg .= "</script>\n";

return $msg;
return $out;
}


/**
* On/off button for constant
*
Expand Down