Listview autocomplete remote jquery
Bonjour,
Je vous explique mon problème, je souhaite mettre en place une recherche autocompleté en jquery mobile.
Code:
1 2 3 4 5
| <h3>Cities worldwide</h3>
<form class="ui-filterable">
<input id="autocomplete-input" data-type="search" placeholder="Recherche un client...">
</form>
<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-input="#autocomplete-input"></ul> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| $( document ).on( "pagecreate", "#myPage", function() {
$( "#autocomplete" ).on( "filterablebeforefilter", function ( e, data ) {
var $ul = $( this ),
$input = $( data.input ),
value = $input.val(),
html = "";
$ul.html( "" );
if ( value && value.length > 2 ) {
$ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
$ul.listview( "refresh" );
$.ajax({
url: "http://gd.geobytes.com/AutoCompleteCity",
dataType: "jsonp",
crossDomain: true,
data: {
q: $input.val()
}
})
.then( function ( response ) {
$.each( response, function ( i, val ) {
html += "<li>" + val + "</li>";
});
$ul.html( html );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
});
}
});
}); |
mon problème maintenant est comment faire pour avoir les données de ma base et non celle de l'exemple, j'ai fait ça mes ça ne marche pas ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| $term=$_GET["term"];
$query=mysql_query("SELECT id_clients, nom_clients FROM clients WHERE nom_clients LIKE '%".$term."%' LIMIT 0, 10");
if ( ! $query )
die ('mysql_query error SELECT clients' . mysql_error());
$json=array();
$return_json=array();
while($row=mysql_fetch_array($query))
{
$row = array_map('utf8_encode', $row);
$critere = $row['code_clients'].' '.$row['nom_clients'].' '.$row['nomdirigeant_clients'];
$json['value'] = $row['nom_clients'];
$json['refVal'] = $row['code_clients'];
$json['prix'] = $row['nomdirigeant_clients'];
$json['id'] = $row['id_clients'];
$json['com'] = $row['type_clients'];
array_push($return_json,$json);
}
echo json_encode($return_json);
flush();
exit; |