Created
June 4, 2021 09:00
-
-
Save MushuLeDragon/b9c8f1643ab3711a2f5b7928c1c2eecb to your computer and use it in GitHub Desktop.
JS / Ajax research for input suggestion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ClassController extends Controller | |
{ | |
public function list_professions(Request $request) | |
{ | |
$em = $this->container->get('doctrine')->getManager(); | |
$type = $request->request->get('type'); | |
$profession_libelle = $request->request->get('profession_libelle'); | |
$liste_professions = ''; | |
$professions = $em->getRepository('App\Entity\Profession')->getProfessionByName($profession_libelle); | |
if (!empty($professions)) { | |
$liste_professions .= "<h5>Merci de sélectionner une profession :</h5><br><ul>"; | |
foreach ($professions as $profession) { | |
$liste_professions .= "<li><a style='cursor:pointer;' class='a-suggest-profession' data-type='" . $type . "' data-profession='" . $profession['pro_libelleprofession'] . "'>" . $profession['pro_libelleprofession'] . "</a></li>"; | |
} | |
$liste_professions .= "</ul>"; | |
} else { | |
$liste_professions .= "<h5>Tapez une profession de votre choix</h5><br>"; | |
} | |
return new Response($liste_professions); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="m-form__group form-group col-lg-2"> | |
<label for="conjoint_profession_libelle">Profession</label> | |
<input class="form-control input m-input--solid profession_libelle" type="text" name="profession_libelle" | |
data-type="conjoint" id="conjoint_profession_libelle" value="{{ conjoint.getProfessionLibelle() }}"> | |
<div id="div_suggestion_profession"></div> | |
</div> | |
<script> | |
var time_localite = null; | |
$(document).on("keyup", ".profession_libelle", function () { | |
if (time_localite) clearTimeout(time_localite); | |
var elem = $(this); | |
time_localite = setTimeout(suggestProfession, 400, elem); | |
}); | |
function suggestProfession(elem) { | |
var type = $(elem).attr('data-type'); | |
var search = $(elem).val().trim(); | |
if (search.length >= 2) { | |
$.ajax({ | |
type: "POST", | |
async: false, | |
url: "{{ path('list_professions') }}", | |
data: { 'profession_libelle': search, 'type': type }, | |
success: function (data) { | |
$("#div_suggestion_profession").show(); | |
$("#div_suggestion_profession").html(data); | |
} | |
}); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment