I need to group AutoComplete results and I've found following solution. How can I figure out the category of selected suggestion?
For example, lets say there are City and Country categories and user selects one of the cities. How am I supposed to know the selected item is part of city and not country category (When the form gets submitted)? I also do not want the category names be visible to users.
What I have found so far
$.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 );
});
}
});
My Code
$(function() {
$("#box1").autocomplete({
source: function(e, r) {
var t, s = "http://localhost:8080/MyProject/autoComplete/box1";
$.ajax({
url: s,
dataType: "json",
data: {
q: e.term
},
success: function(e) {
r(e)
}
})
},
select: function(event, ui) {
if (ui.item) {
alert("box one is seleted");
}
},
}), $("#box2").autocomplete({
source: function(e, r) {
$.ajax({
url: "http://localhost:8080/MyProject/autoComplete/box2",
dataType: "json",
data: {
q: e.term
},
success: function(e) {
r(e)
}
})
},
select: function(event, ui) {
if (ui.item) {
alert("box two is selected");
}
},
})
Update
I've also found this code but could not figure out the category.