Using Jquery Autocomplete, requesting a script.
The list shows as many rows as there are results (when I set my script to return X results, there are X rows as well in the list) :
But it doesn't fill the rows with the data. What could have gone wrong there ?
The data returned is some json :
Request URL:http://localhost:8000/search/autocomplete?term=750
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
Response Headers
view source
Cache-Control:no-cache
Connection:close
Content-Type:application/json
Date:Tue, 15 Nov 2016 14:53:07 GMT
Host:localhost:8000
And here is the data :
[{"zip":"75004","commune":"PARIS 04","libelle":"PARIS","lieudit":""},{"zip":"75005","commune":"PARIS 05","libelle":"PARIS","lieudit":""},{"zip":"75003","commune":"PARIS 03","libelle":"PARIS","lieudit":""},{"zip":"75006","commune":"PARIS 06","libelle":"PARIS","lieudit":""},{"zip":"75008","commune":"PARIS 08","libelle":"PARIS","lieudit":""},{"zip":"75012","commune":"PARIS 12","libelle":"PARIS","lieudit":""},{"zip":"75015","commune":"PARIS 15","libelle":"PARIS","lieudit":""},{"zip":"75016","commune":"PARIS 16","libelle":"PARIS","lieudit":""},{"zip":"75017","commune":"PARIS 17","libelle":"PARIS","lieudit":""},{"zip":"75010","commune":"PARIS 10","libelle":"PARIS","lieudit":""},{"zip":"75018","commune":"PARIS 18","libelle":"PARIS","lieudit":""},{"zip":"75001","commune":"PARIS 01","libelle":"PARIS","lieudit":""},{"zip":"75009","commune":"PARIS 09","libelle":"PARIS","lieudit":""},{"zip":"75014","commune":"PARIS 14","libelle":"PARIS","lieudit":""},{"zip":"75002","commune":"PARIS 02","libelle":"PARIS","lieudit":""},{"zip":"75007","commune":"PARIS 07","libelle":"PARIS","lieudit":""},{"zip":"75011","commune":"PARIS 11","libelle":"PARIS","lieudit":""},{"zip":"75013","commune":"PARIS 13","libelle":"PARIS","lieudit":""},{"zip":"75019","commune":"PARIS 19","libelle":"PARIS","lieudit":""},{"zip":"75020","commune":"PARIS 20","libelle":"PARIS","lieudit":""}]
Here is my JS :
$(function(){
$( "#fromzip" ).autocomplete({
source: "/search/autocomplete",
dataType: 'json',
minLength: 3,
});
});
The HTML :
<input
id="fromzip"
name="fromzip"
type="text"
class="form-control"
placeholder="69003"
pattern=".{5}"
title="5 numbers zip"
maxlength="5"
required >
And the PHP (Laravel Input, DB and Response facades) :
public function autocomplete(){
$term = Input::get('term');
$results = array();
$queries = DB::table('zips')
->where('zip', 'LIKE', $term.'%')
->orWhere('libelle', 'LIKE', $term.'%')
->take(30)->get();
foreach ($queries as $query)
{
$results[] = [ 'zip' => $query->zip,
'commune' => $query->commune,
'libelle' => $query->libelle,
'lieudit' => $query->lieudit];
}
return Response::json($results);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire