I'm using jQuery UI's Auto Complete to provide suggestions from a remote source for a search input box. I've got the "remote datasource" example working. For example, this works:
$("#search").autocomplete({
source: "search_basic.php",
minLength: 2
});
However, I'd like to use the "Categories" example to sort the suggesions by category. The example from the jQuery UI site, with an inline set of data works fine:
<script>
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
self._renderItem( ul, item );
});
}
});
$(function() {
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
$( "#search" ).catcomplete({
delay: 0,
source: data
});
});
</script>
However, when I try to get the data from my remote file
source: 'search.php'
it doesn't suggest anything. Here's the code with the search.php:
<script>
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
self._renderItem( ul, item );
});
}
});
$(function() {
$( "#search" ).catcomplete({
source: 'search.php'
});
});
</script>
The data that search.php is returning is correctly formatted:
[
{ label: "annhhx10", category: "Products" },
{ label: "annttop", category: "Products" },
{ label: "anders", category: "People" },
{ label: "andreas", category: "People" }
]
Any help would be greatly appreciated!
Thanks, Greg