autocomplete ._renderItem and adding a Class to wrapper

天涯浪子 提交于 2019-11-30 07:31:59

问题


Going off the example here http://jqueryui.com/demos/autocomplete/#custom-data I'm wondering how to add a style to the ul wrapper when using _renderItem():

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

回答1:


Here would be one simple way to do it, tapping into the open event:

$("#auto").autocomplete({
    source: /* ... */,
    open: function () {
        $(this).data("autocomplete").menu.element.addClass("my_class");
    }
});

jQueryUI >= 1.9

$("#auto").autocomplete({
    source: /* ... */,
    open: function () {
        $(this).data("uiAutocomplete").menu.element.addClass("my_class");
    }
});

menu is an internal widget that autocomplete uses.

Example: http://jsfiddle.net/bx8Ye/




回答2:


If you want to add a style to the ul wrapper then you need to overload _renderMenu() and not _renderItem().

Here is an example that sets the width of the UL and adds a footer as the last li in the ul

.data( "autocomplete" )._renderMenu = function( ul, data ) {

    var self = this;
    $(ul).css('width', settings.dropDownWidth);

    $.each( data, function( index, item ) {
        self._renderItem( ul, item );
    });

    $(ul).append("<div class='myFooter'>some footer text</div>");
}; 



回答3:


When using jQuery UI 1.10, I used Andrew Whitaker's answer, but I had to change

$(this).data("autocomplete").menu.element.addClass("my_class");

to

$(this).data("uiAutocomplete").menu.element.addClass("my_class");


来源:https://stackoverflow.com/questions/9337796/autocomplete-renderitem-and-adding-a-class-to-wrapper

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