How can I override _renderItem for only #global-search
?
$("#global-search").autoplete({
//
})._renderMenu = function(ul, items) {
var self = this;
ul.append('<table class="ac-search-table"></table>');
$.each( items, function( index, item ) {
self._renderItem( ul.find("table"), item );
});
});
How can I override _renderItem for only #global-search
?
$("#global-search").autoplete({
//
})._renderMenu = function(ul, items) {
var self = this;
ul.append('<table class="ac-search-table"></table>');
$.each( items, function( index, item ) {
self._renderItem( ul.find("table"), item );
});
});
Share
Improve this question
edited Dec 28, 2012 at 16:28
el_pup_le
asked Dec 28, 2012 at 16:21
el_pup_leel_pup_le
12.2k26 gold badges90 silver badges146 bronze badges
4
-
You wanr to override
_renderItem
or_renderMenu
here? – raina77ow Commented Dec 28, 2012 at 16:31 - Both, I have it working like so: $.ui.autoplete.prototype._renderMenu = ... and ..._renderItem BUT I have multiple AC's on the page and only want one of them to have their functions overridden. – el_pup_le Commented Dec 28, 2012 at 16:38
- Then I suppose it's better to follow the approach I've shown in my answer. You can define some generic functionality in your own methods, then override it with instance methods. – raina77ow Commented Dec 28, 2012 at 16:40
- Please help on the following issue stackoverflow./q/33278419/5176876 – bharath Commented Oct 22, 2015 at 10:10
2 Answers
Reset to default 14Remember that you can address the particular instance of the widget created by jQuery UI factory method (_create
) via data
:
var widgetInst = $("#global-search").autoplete({}).data('ui-autoplete');
... or, since jQuery UI 1.12, via instance() helper method:
var widgetInst = $("#global-search").autoplete('instance');
Thus you're able to override its methods with your own:
widgetInst._renderMenu = function(ul, items) {
var self = this;
ul.append('<table class="ac-search-table"></table>');
$.each( items, function( index, item ) {
self._renderItem( ul.find("table"), item );
});
};
You have a couple of options:
You can override the
_renderItem
function so that it does some object parison internally and either calls the original_renderItem
function or executes your custom code.You can create a new widget that inherits from
autoplete
and override the functions there.