Cant override jQuery UI autocomplete renderItem method multiple times
Asked Answered
S

2

8

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

Savagism answered 24/8, 2011 at 18:14 Comment(0)
F
19

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;
}
Funiculate answered 29/9, 2011 at 18:34 Comment(2)
It's a JavaScript Object Literal. Obviously you'd have to get rid of the '....' and the last comma for this example to work.Funiculate
@Funiculate - you just saved me HOURS of extra work. Do the jquery-ui folks now that a workaround is needed for multiple autocomplete elements?Jackstraw
A
10

You can override _renderItem

$.ui.autocomplete.prototype._renderItem = function (ul, item) { ... };
Angi answered 22/5, 2012 at 9:50 Comment(2)
It is recommended to do that way?Caliber
It's a way to do it. Neither recommended nor not recommended.Angi

© 2022 - 2024 — McMap. All rights reserved.