javascript 覆盖 _renderItem 和 _renderMenu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14072487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:38:12  来源:igfitidea点击:

Override both _renderItem and _renderMenu

javascriptjqueryjquery-ui

提问by el_pup_le

How can I override _renderItem for only #global-search?

如何仅覆盖 _renderItem #global-search

$("#global-search").autocomplete({
   //       
})._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 );
   });
});

回答by raina77ow

Remember that you can address the particular instance of the widget created by jQuery UI factory method (_create) via data:

请记住,您可以通过以下方式处理由 jQuery UI 工厂方法 ( _create)创建的小部件的特定实例data

var widgetInst = $("#global-search").autocomplete({}).data('ui-autocomplete');

... or, since jQuery UI 1.12, via instance()helper method:

... 或者,从 jQuery UI 1.12 开始,通过instance()辅助方法:

var widgetInst = $("#global-search").autocomplete('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 );
  });
};

回答by Jason Whitted

You have a couple of options:

您有几个选择:

  1. You can override the _renderItemfunction so that it does some object comparison internally and either calls the original _renderItemfunction or executes your custom code.

  2. You can create a new widget that inherits from autocompleteand override the functions there.

  1. 您可以覆盖该_renderItem函数,以便它在内部进行一些对象比较,并调用原始_renderItem函数或执行您的自定义代码。

  2. 您可以创建一个新的小部件,它继承autocomplete并覆盖那里的功能。