I use the autocomplete function of jQuery UI.
$("#search").autocomplete({
minLength: 0,
source: 'source.php',
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
I insert multiple elements in the source.php and return them json encoded.
$search[] = array(
'value' => $id,
'label' => $name
);
echo json_encode($search);
When I start typing into the autocomplete field a list is shown with the elements of the source.php. But unfortunately with all of them. They are not filtert depending on what I enter in the field.
Is there any special option I have to set when I work with json?
EDIT: Thanks to T.J. Crowder I came up with this solution to let jQuery do the job ; )
$.getJSON('source.php', function(search) {
$("#search").autocomplete({
minLength: 0,
source: search,
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
It’s not obvious from the docs, but when you supply anything as
sourcethat will involve running your code (either server- or client-side), the jQuery UI autocompleter expects you to filter the result. In the case of server-side code, you’d use thetermparameter it passes to your PHP file. From the docs:(It would be good if they actually mentioned filtering there; I’ve logged an issue suggesting that they do. Update: It took them less than three hours to update the docs and close the issue; new docs will be pushed at some point, at least by v1.9. Nice!)
The autocompleter allows you to supply sources in three ways:
Static source array: In this case, the autocompleter does the filtering.
Server-side call: In this case, it passes a
termargument and you’re expected to use it to filter.Client-side call: In this case, it passes a
requestobject to your client-side code that has atermproperty; you’re expected to use that to filter.