Skip to content
Snippets Groups Projects
Commit 3925ae07 authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Fix: we loose ability to refresh contact list when changing a company on

contact tab
parent 2fd01d71
Branches
Tags
No related merge requests found
...@@ -564,16 +564,21 @@ class FormCompany ...@@ -564,16 +564,21 @@ class FormCompany
} }
} }
// Refresh contacts list on thirdparty list change
$htmloption='';
$events=array(); $events=array();
// Add an entry 'method' to say 'yes, we must execute url with param action = method';
// Add an entry 'url' to say which url to execute
// Add an entry htmlname to say which element we must change once url is called
// Add entry params => array('cssid' => 'attr') to say to remov or add attribute attr if answer of url return 0 or >0 lines
// To refresh contacts list on thirdparty list change
$events[]=array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php',1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled')); $events[]=array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php',1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled'));
if (count($events)) // If there is some ajax events to run once selection is done, we add code here to run events if (count($events)) // If there is some ajax events to run once selection is done, we add code here to run events
{ {
print '<script type="text/javascript"> print '<script type="text/javascript">
jQuery(document).ready(function() { jQuery(document).ready(function() {
$("#search_'.$htmlname.'").change(function() { $("#search_'.$htmlname.'").change(function() {
console.log("Call runJsCodeForEvent'.$htmlname.'");
var obj = '.json_encode($events).'; var obj = '.json_encode($events).';
$.each(obj, function(key,values) { $.each(obj, function(key,values) {
if (values.method.length) { if (values.method.length) {
...@@ -609,6 +614,7 @@ class FormCompany ...@@ -609,6 +614,7 @@ class FormCompany
} }
} }
}); });
/* console.log("Change select#"+htmlname+" with content "+response.value) */
$("select#" + htmlname).html(response.value); $("select#" + htmlname).html(response.value);
} }
} }
...@@ -619,8 +625,8 @@ class FormCompany ...@@ -619,8 +625,8 @@ class FormCompany
} }
print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n"; print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n";
print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$name.'" '.$htmloption.' />'; print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$name.'" />';
print ajax_autocompleter(($socid?$socid:-1),$htmlname,DOL_URL_ROOT.'/societe/ajaxcompanies.php','',$minLength); print ajax_autocompleter(($socid?$socid:-1), $htmlname, DOL_URL_ROOT.'/societe/ajaxcompanies.php', '', $minLength, 0);
return $socid; return $socid;
} }
else else
......
...@@ -35,29 +35,40 @@ ...@@ -35,29 +35,40 @@
* @param string $urloption More parameters on URL request * @param string $urloption More parameters on URL request
* @param int $minLength Minimum number of chars to trigger that Ajax search * @param int $minLength Minimum number of chars to trigger that Ajax search
* @param int $autoselect Automatic selection if just one value * @param int $autoselect Automatic selection if just one value
* @param array $ajaxoptions Multiple options array (Ex: array('update'=>array('field1','field2'...)) will reset field1 and field2 once select done) * @param array $ajaxoptions Multiple options array
* Ex: array('update'=>array('field1','field2'...)) will reset field1 and field2 once select done
* Ex: array('disabled'=>
* Ex: array('show'=>
* Ex: array('update_textarea'=>
* @return string Script * @return string Script
*/ */
function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array()) function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLength=2, $autoselect=0, $ajaxoptions=array())
{ {
if (empty($minLength)) $minLength=1; if (empty($minLength)) $minLength=1;
// Input search_htmlname is original field
// Input htmlname is a second input field used when using ajax autocomplete.
$script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />'; $script = '<input type="hidden" name="'.$htmlname.'" id="'.$htmlname.'" value="'.$selected.'" />';
$script.= '<script type="text/javascript">'; $script.= '<!-- Javascript code for autocomplete of field '.$htmlname.' -->'."\n";
$script.= '<script type="text/javascript">'."\n";
$script.= '$(document).ready(function() { $script.= '$(document).ready(function() {
var autoselect = '.$autoselect.'; var autoselect = '.$autoselect.';
var options = '.json_encode($ajaxoptions).'; var options = '.json_encode($ajaxoptions).';
// Remove product id before select another product /* Remove product id before select another product use keyup instead of change to avoid loosing the product id. This is needed only for select of predefined product */
// use keyup instead change to avoid loosing the product id /* TODO Check if we can remove this */
$("input#search_'.$htmlname.'").keydown(function() { $("input#search_'.$htmlname.'").keydown(function() {
$("#'.$htmlname.'").val(""); $("#'.$htmlname.'").val("");
}); });
/* I disable this. A call to trigger is already done later into the select action of the autocomplete code
$("input#search_'.$htmlname.'").change(function() { $("input#search_'.$htmlname.'").change(function() {
console.log("Call the change trigger on input '.$htmlname.' because of a change on search_'.$htmlname.' was triggered");
$("#'.$htmlname.'").trigger("change"); $("#'.$htmlname.'").trigger("change");
}); });*/
// Check when keyup
// Check options for secondary actions when keyup
$("input#search_'.$htmlname.'").keyup(function() { $("input#search_'.$htmlname.'").keyup(function() {
if ($(this).val().length == 0) if ($(this).val().length == 0)
{ {
...@@ -123,7 +134,8 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt ...@@ -123,7 +134,8 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt
}, },
dataType: "json", dataType: "json",
minLength: '.$minLength.', minLength: '.$minLength.',
select: function( event, ui ) { // Function ran when new value is selected into javascript combo select: function( event, ui ) { // Function ran once new value has been selected into javascript combo
console.log("Call change on input '.$htmlname.' because of select definition of autocomplete select call on input#search_'.$htmlname.'");
$("#'.$htmlname.'").val(ui.item.id).trigger("change"); // Select new value $("#'.$htmlname.'").val(ui.item.id).trigger("change"); // Select new value
// Disable an element // Disable an element
if (options.option_disabled) { if (options.option_disabled) {
...@@ -167,6 +179,8 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt ...@@ -167,6 +179,8 @@ function ajax_autocompleter($selected, $htmlname, $url, $urloption='', $minLengt
} }
}); });
} }
console.log("ajax_autocompleter new value selected, we trigger change on original component");
$("#search_'.$htmlname.'").trigger("change"); // We have changed value of the combo select, we must be sure to trigger all js hook binded on this event. This is required to trigger other javascript change method binded on original field by other code.
} }
,delay: 500 ,delay: 500
}).data("ui-autocomplete")._renderItem = function( ul, item ) { }).data("ui-autocomplete")._renderItem = function( ul, item ) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment