Fix js refresh with autocomplete

This commit is contained in:
Laurent Destailleur 2015-06-20 17:23:28 +02:00
parent ba5d6318ef
commit 32626c5ae6

View File

@ -225,8 +225,8 @@ function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLengt
} }
for (i=0;i<nboffields;i++) { for (i=0;i<nboffields;i++) {
if (item[fields[i]]) { // If defined if (item[fields[i]]) { // If defined
//alert(item[fields[i]]); //alert(item[fields[i]]);
jQuery("#" + fields[i]).val(item[fields[i]]); jQuery("#" + fields[i]).val(item[fields[i]]);
} }
} }
} }
@ -235,13 +235,22 @@ function ajax_multiautocompleter($htmlname, $fields, $url, $option='', $minLengt
}); });
}, },
select: function( event, ui ) { select: function( event, ui ) {
needtotrigger = "";
for (i=0;i<nboffields;i++) { for (i=0;i<nboffields;i++) {
//alert(fields[i] + " = " + ui.item[fields[i]]); //alert(fields[i] + " = " + ui.item[fields[i]]);
if (fields[i]=="selectcountry_id") if (fields[i]=="selectcountry_id")
{ {
if (ui.item[fields[i]] > 0) // Do not erase country if unknown if (ui.item[fields[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]]); 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 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) { if (ui.item.states && ui.item["state_id"] != jQuery("#state_id").value) {
jQuery("#state_id").html(ui.item.states); 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 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 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 else if (ui.item[fields[i]]) { // If defined
//alert(fields[i]); oldvalue=jQuery("#" + fields[i]).val();
//alert(ui.item[fields[i]]); newvalue=ui.item[fields[i]];
//alert(oldvalue+" "+newvalue);
jQuery("#" + fields[i]).val(ui.item[fields[i]]); 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");
});
}
} }
}); });
});'; });';