Making jQuery UI's Autocomplete widget *actually* autocomplete

后端 未结 2 1940
死守一世寂寞
死守一世寂寞 2020-12-08 12:33

I need an auto-complete in the app I\'m working on and, since I\'m already using jQuery UI, I\'m trying to make its Autocomplete widget meet my needs.

Step one is to

相关标签:
2条回答
  • 2020-12-08 13:11

    Took the length checking portion and modified the filter function within the search. This way you can take advantage of the UI's JSON handling.

        filter: function(array, term) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
    
        var smatcher = term.toLowerCase();
        var length = smatcher.length;
    
    
        return $.grep( array, function(value) {
                    if(value.label.substring(0, length).toLowerCase() == smatcher){
                        return matcher.test( value.label || value.value || value );
                    }
        });
    
    0 讨论(0)
  • 2020-12-08 13:13

    Got it. I'd forgotten that widgets can be accessed via .data().

    $("#field").autocomplete({
        delay: 0,
    
        source: function filter_realms(request, response) {
            var term = request.term.toLowerCase();
            var length = term.length;
    
            response($.grep(candidates, function(candidate) {
                return candidate.substring(0, length).toLowerCase() === term;
            }));
        },
    
        open: function(event, ui) {
            var current = this.value;
            var suggested = $(this).data("autocomplete").menu.element.find("li:first-child a").text();
    
            this.value = suggested;
            this.setSelectionRange(current.length, suggested.length);
        }
    });
    
    0 讨论(0)
提交回复
热议问题