I'm trying to use the jQuery autocomplete plugin, with a limited height and 'overflow-x: auto;' set for the results. I do a search, and it loads data as it should. The problem is that as soon as I start scrolling when results exceed the maximum height, the results div disappears. Clicking on results fills the text box as expected.
What I'm doing is similar to the following: http://jqueryui.com/demos/autocomplete/#maxheight
The only differences I have are some CSS to style it, some live events that handle highlighting results by adding/removing CSS classes, and the monkey patch included here to add result highlighting: jQueryUI: how can I custom-format the Autocomplete plug-in results?
Commenting these changes out does nothing. Code is as follows:
function monkeyPatchAutocomplete() {
// Adapted from code on https://stackoverflow.com/questions/2435964/jqueryui-how-can-i-custom-format-the-autocomplete-plug-in-results
// don't really need this, but in case I did, I could store it and chain
var oldFn = $.ui.autocomplete.prototype._renderItem;
$.ui.autocomplete.prototype._renderItem = function( ul, item) {
var re_match = new RegExp(this.term, "ig");
var t = item.label.replace(re_match,"<span style='font-weight:bold;'>" +
this.term +
"</span>");
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + t + "</a>" )
.appendTo( ul );
};
}
$(document).ready(function() {
monkeyPatchAutocomplete();
$.getJSON("cgi-bin/script.pl?return_type=search", function(json) {
$.each(json.results, function(i,result) {
var result_string = result.string1 + "/" + result.string2 + "/" + result.string3;
arr_results.push(result_string);
});
$("input#search_box").autocomplete({ minLength: 2, source: arr_results });
});
$("li.ui-menu-item").live("mouseover", function() {
if( $(this).has("a.ui-state-hover") ) {
$(this).addClass("autocomplete-hover");
}
});
$("li.ui-menu-item").live("mouseout", function() {
$(this).removeClass("autocomplete-hover");
});
}
and the CSS:
ul.ui-autocomplete {
background-color: lightblue;
border: 1px solid black;
width: 300px;
font-size: 14px;
max-height: 300px;
overflow-y: auto;
overflow-x: hidden;
padding: 0;
margin: 0;
}
li.ui-menu-item {
list-style-type: none;
}
.autocomplete-hover {
background-color: skyblue;
cursor: pointer;
}
Any suggestions from jQuery/jQuery-UI pros would be welcomed.