I stumbled upon this thread as I was seeking a way to prefill a bunch of jquery ui autocomplete fields in a form where users can modify an already submitted one.
First, I have to load the form, with some inputs using autocomplete.
Problem 1 : I created a php function able to call a premade autocomplete input, specifying some things like the input class, id, name... and value(s). I can also pass the name of a JS custom function to trigger (with the selected item's ID as a parameter), depending on the form and type of data... So I can't just "print" the prefilled input when loading the form because the custom function (and other actions specified in the built-in select: function( event, ui ) { ... } won't fire.
I really need to mimic a search-->click action.
Problem 2 : the source comes from an ajax call performing a search from the string entered in the input. It then shows strings as items... but I want the real input to be the IDs of those sources. To do so, the select: event creates a hidden input with the ID of the clicked item, checks if it was already selected (I can select multiple elements from one autocomplete input..), etc. It has to be triggered.
I really need to mimic a search-->click action.
Problem 3 : Since the source comes from an ajax call, I would have to make all these calls to prefill the form, which is a very bad way of populating a form (for various reasons).
I can't really mimic the search-->click action.
Solution
I simply created a second (hidden) autocomplete input for each "real ones", for which the initialisation $(id).autocomplete(...) performs the same actions (upon select), but is populated with static source i drew directly from the DB with the already saved input. Then, I simply mimic the search-->click in this dummy input, which draws the good inputs with the IDs I want to save...
foreach (values to prefill..) {
$("#'.$mot_cle.'_input_autocomplete_prefill").val("'.$valeur_finale_unique['value'].'");
$("#'.$mot_cle.'_input_autocomplete_prefill").autocomplete("search", "'.$valeur_finale_unique['value'].'");
var list = $("#'.$mot_cle.'_input_autocomplete_prefill").autocomplete("widget");
$(list[0].children[0]).click();
}
It's quite complicated. But in the end, everytime I want to show an autocomplete input in my pages, I only have to call the php function which wraps it up ;)
And it's lightning fast, and don't mess with the input IDs\Names for the entire forms managing...
There is a lot going on backend, but in the end, it looks like this, when I enter this :
<div class="marge">
<span class="texte_gras">Demandeur(s)</span><br>
'.formulaire_autocomplete("demandeurs","pdrh_formulaire_ajouter_cible_valeurs","1","usager",$valeur_demandeur,"").'
</div>
PHP function formulaire_autocomplete($keyword(used for input name),$class(used for jquery serialize when submitting form),$multi(possibility to input more than one choice),$type(which DB table to search: users, courses, etc..),$value(IDs to prefill in the form for this input, if it is an already submitted form), $function(if I want to trigger a custom JS function when select:))
I get this :
<div class="marge">
<span class="texte_gras">Demandeur(s)</span><br>
<input style="width: 90%;" class="pdrh_formulaire_ajouter_cible_valeurs ui-autocomplete-input" type="text" name="demandeurs[]" id="demandeurs_input_autocomplete" value="" autocomplete="off"> <input style="width: 90%; display: none;" class="pdrh_formulaire_ajouter_cible_valeurs ui-autocomplete-input" type="text" name="demandeurs_prefill[]" id="demandeurs_input_autocomplete_prefill" value="" autocomplete="off"> <div id="demandeurs_boite_autocomplete_selection" style="width: 100%;"><div style="width: 100%;" id="demandeurs_auto_ID_25434"><div class="petite_marge"><input style="width: 80%;" class="pdrh_formulaire_ajouter_cible_valeurs" type="hidden" name="demandeurs_auto_ID[]" id="demandeurs_input_autocomplete_auto_id_25434" value="25434"><span class="petit_texte texte_gras">STEPHANIE (41263)</span><div class="bouton_transparent" onclick="effacer_div('demandeurs_auto_ID_25434')"><div class="petite_marge"><img src="./images/vide.png" style="width: 1em;"><img src="./images/effacer.png" style="width: 1em;"></div></div></div></div><div style="width: 100%;" id="demandeurs_auto_ID_18760"><div class="petite_marge"><input style="width: 80%;" class="pdrh_formulaire_ajouter_cible_valeurs" type="hidden" name="demandeurs_auto_ID[]" id="demandeurs_input_autocomplete_auto_id_18760" value="18760"><span class="petit_texte texte_gras">MARIE (23673)</span><div class="bouton_transparent" onclick="effacer_div('demandeurs_auto_ID_18760')"><div class="petite_marge"><img src="./images/vide.png" style="width: 1em;"><img src="./images/effacer.png" style="width: 1em;"></div></div></div></div></div><script>
$("#demandeurs_input_autocomplete").autocomplete({
source: function (request, response) {
requete_ajax = $.ajax({
type: "POST",
url: 'fonctions.php',
data: 'recherche_autocomplete=usager&chaine='+request.term,
dataType: 'json',
success: function(data) {
ajax_en_cours = 0; console.log("Ajax terminé");
// var resultat = JSON.parse(data.choix);
var tableau_resultat = Object.values(data.choix);
response(tableau_resultat);
console.log(tableau_resultat);
},
error: function(e) {
ajax_en_cours = 0; console.log("Ajax terminé");
console.log('Erreur | demandeurs recherche autocomplete');
}
});
},
minLength: 3,
select: function( event, ui ) {
$("#demandeurs_input_autocomplete_auto_id").val(ui.item.ID);
var contenu_selection = $("#demandeurs_boite_autocomplete_selection").html();
$("#demandeurs_boite_autocomplete_selection").html(contenu_selection+"<div style=\"width: 100%;\" id=\"demandeurs_auto_ID_"+ui.item.ID+"\"><div class=\"petite_marge\"><input style=\"width: 80%;\" class=\"pdrh_formulaire_ajouter_cible_valeurs\" type=\"hidden\" name=\"demandeurs_auto_ID[]\" id=\"demandeurs_input_autocomplete_auto_id_"+ui.item.ID+"\" value=\""+ui.item.ID+"\"><span class=\"petit_texte texte_gras\">"+ui.item.value+"</span><div class=\"bouton_transparent\" onclick=\"effacer_div('demandeurs_auto_ID_"+ui.item.ID+"')\"><div class=\"petite_marge\"><img src=\"./images/vide.png\" style=\"width: 1em;\"><img src=\"./images/effacer.png\" style=\"width: 1em;\"></div></div></div></div>");
$("#demandeurs_input_autocomplete").val(" ");
}
});
var valeurs_prefill = [{value: "STEPHANIE (41263)", ID: "25434"},{value: "MARIE (23673)", ID: "18760"}];
$("#demandeurs_input_autocomplete_prefill").autocomplete({
source: valeurs_prefill,
minLength: 3,
select: function( event, ui ) {
$("#demandeurs_input_autocomplete_auto_id").val(ui.item.ID);
var contenu_selection = $("#demandeurs_boite_autocomplete_selection").html();
$("#demandeurs_boite_autocomplete_selection").html(contenu_selection+"<div style=\"width: 100%;\" id=\"demandeurs_auto_ID_"+ui.item.ID+"\"><div class=\"petite_marge\"><input style=\"width: 80%;\" class=\"pdrh_formulaire_ajouter_cible_valeurs\" type=\"hidden\" name=\"demandeurs_auto_ID[]\" id=\"demandeurs_input_autocomplete_auto_id_"+ui.item.ID+"\" value=\""+ui.item.ID+"\"><span class=\"petit_texte texte_gras\">"+ui.item.value+"</span><div class=\"bouton_transparent\" onclick=\"effacer_div('demandeurs_auto_ID_"+ui.item.ID+"')\"><div class=\"petite_marge\"><img src=\"./images/vide.png\" style=\"width: 1em;\"><img src=\"./images/effacer.png\" style=\"width: 1em;\"></div></div></div></div>");
$("#demandeurs_input_autocomplete").val(" ");
}
});
$("#demandeurs_input_autocomplete_prefill").val("STEPHANIE (41263)");
$("#demandeurs_input_autocomplete_prefill").autocomplete("search", "STEPHANIE (41263)");
var list = $("#demandeurs_input_autocomplete_prefill").autocomplete("widget");
$(list[0].children[0]).click();
$("#demandeurs_input_autocomplete_prefill").val("MARIE (23673)");
$("#demandeurs_input_autocomplete_prefill").autocomplete("search", "MARIE (23673)");
var list = $("#demandeurs_input_autocomplete_prefill").autocomplete("widget");
$(list[0].children[0]).click();
</script>
</div>