jQuery UI Autocomplete with Categories
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
Since I migrated to UI 1.10.2 my widget did'nt work !
Just a modification in the line :
self._renderItem( ul, item );
that becomes :
self._renderItemData( ul, item );
That works again !
Your PHP file probably isn't returning the right header. Add this to your PHP file:
header('Content-Type: application/json');
The browser will then interpret the response as JSON and act on it.
EDIT:
Your response also needs to have quotes around the labels, not just the values, when returning JSON in a response.
In PHP, using json_encode()
on an array of objects will return the following JSON (linebreaks added):
[
{ "label": "annhhx10", "category": "Products" },
{ "label": "annttop", "category": "Products" },
{ "label": "anders", "category": "People" },
{ "label": "andreas", "category": "People" }
]
精彩评论