Add scroll event to jquery ui autocomplete
Once scroll to the bottom of scrollbar, it will load more data.
by chemark
HTML
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/themes/cupertino/jquery-ui.css">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
CSS
.ui-autocomplete {
max-height: 100px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
/* add padding to account for vertical scrollbar */
padding-right: 20px;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autocomplete {
height: 100px;
}
JavaScript
$(function () {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme",
"AA",
"BB",
"CC",
"DD",
"EE",
"FF",
"GG",
"HH",
"II",
"JJ",
"KK"
];
$.extend($.ui.autocomplete.prototype, {
_renderMenu: function (ul, items) {
//remove scroll event to prevent attaching multiple scroll events to one container element
$(ul).unbind("scroll");
var self = this;
self._scrollMenu(ul, items);
},
_scrollMenu: function (ul, items) {
var self = this;
var maxShow = 5;
var results = [];
var pages = Math.ceil(items.length / maxShow);
results = items.slice(0, maxShow);
if (pages > 1) {
$(ul).scroll(function () {
if (isScrollbarBottom($(ul))) {
++window.pageIndex;
if (window.pageIndex >= pages) return;
results = items.slice(window.pageIndex * maxShow, window.pageIndex * maxShow + maxShow);
//append item to ul
...