Cant override jQuery UI autocomplete renderItem method multiple times

被刻印的时光 ゝ 提交于 2019-12-05 22:00:58

问题


It overrides properly in the first autocomplete found, but do nothing with the rest. Instead it loads the original _renderitem method that you can see at https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.autocomplete.js#L449.

$(".someClassWithMultipleItemsOnDOM").autocomplete({
        delay:500,
        minLength:2,
        source:path"
        .....   
}).data( "autocomplete" )._renderItem = function( ul, item ) {

thanks in advance


回答1:


There's a workaround for this problem:

var autoc = {
    delay: 500,
    minLength: 2,
    source: path,
    .....   
};

var renderItem = function (ul, item) {
    return $("<li></li>")
        .data("item.autocomplete", item)
        .append("<a>" + item.label + "<br/>" + item.desc + "</a>")
        .appendTo(ul);
};

$(".someClassWithMultipleItemsOnDOM").each(function (i) {
     $(this).autocomplete(autoc).data("autocomplete")._renderItem = renderItem;
}



回答2:


You can override _renderItem

$.ui.autocomplete.prototype._renderItem = function (ul, item) { ... };


来源:https://stackoverflow.com/questions/7180246/cant-override-jquery-ui-autocomplete-renderitem-method-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!