From 32626c5ae6ad488716ede7770d4dce87ac9416b3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 20 Jun 2015 17:23:28 +0200 Subject: [PATCH] Fix js refresh with autocomplete --- htdocs/core/lib/ajax.lib.php | 37 ++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php index db47ad041e2..4d94906a0bf 100644 --- a/htdocs/core/lib/ajax.lib.php +++ b/htdocs/core/lib/ajax.lib.php @@ -225,8 +225,8 @@ function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLengt } for (i=0;i 0) // Do not erase country if unknown { + oldvalue=jQuery("#" + fields[i]).val(); + newvalue=ui.item[fields[i]]; + //alert(oldvalue+" "+newvalue); jQuery("#" + fields[i]).val(ui.item[fields[i]]); + if (oldvalue != newvalue) // To force select2 to refresh visible content + { + needtotrigger="#" + fields[i]; + } + // If we set new country and new state, we need to set a new list of state to allow change if (ui.item.states && ui.item["state_id"] != jQuery("#state_id").value) { jQuery("#state_id").html(ui.item.states); @@ -252,15 +261,35 @@ function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLengt { if (ui.item[fields[i]] > 0) // Do not erase state if unknown { + oldvalue=jQuery("#" + fields[i]).val(); + newvalue=ui.item[fields[i]]; + //alert(oldvalue+" "+newvalue); jQuery("#" + fields[i]).val(ui.item[fields[i]]); // This may fails if not correct country + if (oldvalue != newvalue) // To force select2 to refresh visible content + { + needtotrigger="#" + fields[i]; + } } } else if (ui.item[fields[i]]) { // If defined - //alert(fields[i]); - //alert(ui.item[fields[i]]); + oldvalue=jQuery("#" + fields[i]).val(); + newvalue=ui.item[fields[i]]; + //alert(oldvalue+" "+newvalue); jQuery("#" + fields[i]).val(ui.item[fields[i]]); + if (oldvalue != newvalue) // To force select2 to refresh visible content + { + needtotrigger="#" + fields[i]; + } } } + if (needtotrigger != "") // To force select2 to refresh visible content + { + // We introduce a delay so hand is back to js and all other js change can be done before the trigger that may execute a submit is done + // This is required for example when changing zip with autocomplete that change the country + jQuery(needtotrigger).delay(500).queue(function() { + jQuery(needtotrigger).trigger("change"); + }); + } } }); });';