This is the solution I went with. Works great.
$.widget("ui.addResultList", {
_init: function() {
var constructor = $('<ul></ul>'),
input = $('<input type="hidden" />'),
offset = $(this.element).offsetParent();
constructor.addClass(this.options.className);
input.attr('id', this.options.className + '_inputid');
input.attr('name', this.options.className + '_inputid');
constructor.css({
display: 'none',
left : offset.left + 'px',
position : 'absolute',
top : offset.top + 'px',
zIndex : '10'
});
this.constructor = constructor;
this.keyListner = false;
modalObjects.push(this.constructor);
$(this.constructor).hover(function () {
mouse_is_inside = true;
}, function () {
mouse_is_inside = false;
});
$(this.element).after(input);
$(this.element).after(constructor);
},
_updateList : function (data) {
var r = data.result,
l = r.length,
c = this.constructor,
obj,
i;
c.html('');
for (i = 0; i < l; (i += 1)){
obj = r[i];
if (i === 0) {
c.append('<li id="result_' + obj.id + '" class="selected"><a href="javascript:void(0)">' + obj.name + '</a></li>');
} else {
c.append('<li id="result_' + obj.id + '"><a href="javascript:void(0)">' + obj.name + '</a></li>');
}
}
},
_addKeyEvents : function () {
var _this = this,
old,
scrollY = 0;
this.keyListner = true;
this.element.keydown(function (e) {
old = _this.constructor.find('.selected');
scrollY = 42;
if (_this.constructor.is(':visible') && (e.keyCode === 40 || e.keyCode === 38)) {
if (e.keyCode === 40) {
if (old.is(':last-child')) { return; }
old.next('li').addClass('selected');
scrollY += old.next('li').height();
} else if (e.keyCode === 38) {
if (old.is(':first-child')) { return; }
old.prev('li').addClass('selected');
}
old.prevAll().each(function (index, elem) {
scrollY += $(elem).height();
});
scrollY -= _this.constructor.height();
_this.constructor.get(0).scrollTop = scrollY;
old.removeClass('selected');
}
if (e.keyCode === 13) {
e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
$('#' + _this.constructor.attr('class') + '_inputid').val(old.attr('id').split('_')[1]);
_this.element.val(old.find('a').html());
_this.close();
return false;
}
});
this.constructor.find('li a').live('click', function () {
$('#' + _this.constructor.attr('class') + '_inputid').val($(this.parentNode).attr('id').split('_')[1]);
_this.element.val($(this).html());
_this.close();
});
this.constructor.find('li a').live('mouseover', function () {
_this.constructor.find('li').removeClass('selected');
$(this.parentNode).addClass('selected');
});
},
open : function () {
this.constructor.get(0).scrollTop = 0;
if (arguments[0].data.result.length) {
this._updateList(arguments[0].data);
if (!this.keyListner) {
this._addKeyEvents();
}
this.constructor.show();
} else {
this.close();
}
},
close : function () {
this.constructor.hide();
},
destroy : function() {
$.widget.prototype.apply(this, arguments); // default destroy
}
});
$.extend($.ui.addResultList, {
getter: "value length",
defaults: {
className: 'related_result_list'
}
});
And and a new list with:
$('#related_project_name').addResultList();
And open it with:
$('#related_project_name').keyup(function (e) {
if (e.keyCode === 13) {
e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
return false;
}
if (e.keyCode !== 38 && e.keyCode !== 40) {
if ($(this).val().length > 1) {
model.jso.getConstructions($(this).val(), view.related.showProductList);
} else {
$('#related_project_name').addResultList('close');
}
}
});
Hope is help to anyone with the same problem. :)
..fredrik