What I want to do is to classify the results using autocomplete through the jQueryUI function. After some googling etc. I found that it has a built-in function (http://jqueryui.com/demos/autocomplete/#categories), but this example is only for a local data source (array in javascript). I am dealing with a remote data source. The autocomplete side works fine without adding a category code, but breaks when it is added.
This means that the PHP code is ok (a search page that returns json data).
I took this code from jQueryUI site:
$.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 ); });
Then I combined it with the search side (changing the category):
$(function() { $( "#search" ).catcomplete({ delay:0, source: "query/encode-json.php?term="+ $("#search").val(), select: function(event, ui){ alert(ui.item.label); } }); });
But this does not work :( I searched a lot, but everyone else had problems on the part of json. Here is my json data:
[{"value":"some dude","id":"1","category":"artist"},{"value":"some other dude","id":"2","category":"artist"}]