I have implemented a Laravel 5.1 autocomplete function using following codes:
snippet js
<script type="text/javascript">
$(document).ready(function () {
$("#utente").autocomplete({
minLength:3,
autoFocus: true,
source: '',
select: function( event, ui ) {
window.location.href = ui.item.url;
},
});
});
</script>
This is my Route:
Route::any('utente', function(){
$term = Input::get('utente');
// 4: check if any matches found in the database table
$data = DB::table("tb_users")->distinct()->where('last_name', 'LIKE', $term.'%')->groupBy('last_name')->get();
foreach ($data as $v) {
$nome = $v->last_name . ' ' . $v->first_name;
$url = "clienti?utente=$v->id";
$return_array[] = array('value' => $nome, 'label' => $nome, 'url' => $url); }
// if matches found it first create the array of the result and then convert it to json format so that
// it can be processed in the autocomplete script
return Response::json($return_array);
});
And this is the fiedl used for rendere the autocomplete.
<input type="text" id="utente" name="utente" placeholder="Cognome e Nome" class="form-control-utente" ></input>
The problem is that this fetch all the results in DB... and not the results that match the query
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire