How to patch *just one* instance of Autocomplete on a page?

前端 未结 3 1705
生来不讨喜
生来不讨喜 2020-12-13 10:29

This answer -- jQueryUI: how can I custom-format the Autocomplete plug-in results? -- describes how to monkeypatch the jqueryUI autocomplete widget, so that it displays th

相关标签:
3条回答
  • 2020-12-13 10:38

    Check out the custom data and display demo. This demo is not modifying the prototype object of the autocomplete widget, meaning that only that instance of the widget is effected:

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

    Here's a working demo: http://jsfiddle.net/vJSwq/

    0 讨论(0)
  • 2020-12-13 10:40

    For recent versions of jQuery(1.8+) / jQuery UI (1.10+), you should use:

    $("selector").autocomplete({ ... }).data('uiAutocomplete')._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
        .appendTo( ul );
    };
    
    0 讨论(0)
  • 2020-12-13 10:52

    The code from Andrew Whitaker just work for one autocomplete input. If you select more than one input-element, the second autocomplete widget dosn't show any entrys. You have to add a foreach to handel all selected input elements like mentioned here

    $("selector")
    .autocomplete({ ... })
    .each(function () {
        $(this).data("autocomplete")._renderItem = function( ul, item ) {
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>" + item.label + "<br>" + item.desc + "</a>")
                .appendTo(ul);
        };
    );
    
    0 讨论(0)
提交回复
热议问题