views:

1200

answers:

5

I recently migrated a few of my Autocomplete plugins from the one produced by bassistance to the jQuery UI autocomplete.

Does anyone know how to implement the "mustMatch" and "selectFirst" with just callbacks and other options without modifying the core autocomplete code itself?

+2  A: 

I think I got the mustMatch working with this code... need throughly test though:

<script type="text/javascript">
    $(function() {
        $("#my_input_id").autocomplete({
            source: '/get_my_data/',
            minChars: 3,
            change: function(event, ui) {
                // provide must match checking if what is in the input
                // is in the list of results. HACK!
                var source = $(this).val();
                var found = $('.ui-autocomplete li').text().search(source);
                console.debug('found:' + found);
                if(found < 0) {
                    $(this).val('');
                }
            }
        });
    });
</script>
Esteban Feldman
+9  A: 

I think I solved both features...

To make things easier, I used a common custom selector:

$.expr[':'].textEquals = function (a, i, m) {
    return $(a).text().match("^" + m[3] + "$");
};

The rest of the code:

$(function () {
    $("#tags").autocomplete({
        source: '/get_my_data/',
        change: function (event, ui) {
            //if the value of the textbox does not match a suggestion, clear its value
            if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
                $(this).val('');
            }
        }
    }).live('keydown', function (e) {
        var keyCode = e.keyCode || e.which;
        //if TAB or RETURN is pressed and the text in the textbox does not match a suggestion, set the value of the textbox to the text of the first suggestion
        if((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
            $(this).val($(".ui-autocomplete li:visible:first").text());
        }
    });
});
Doc Hoffiday
Were you also able to get "autoFill" working? I've got a question here http://stackoverflow.com/questions/2933713/add-autofill-capabilities-to-jquery-ui-1-8-1 but I can't for the life of me get autofill to work.
rockinthesixstring
How could I customize this to use the ui object that is passed along side the event parameter in focus, select, etc of the plugin?
Gazillion
+1  A: 

Found this question to be useful.

Thought i'd post up the code i'm now using (adapted from answer above).

I've added my own mustMatch option, and css class to highlight the issue before resetting the textbox value.

       change: function (event, ui) {
          if (options.mustMatch) {
            var found = $('.ui-autocomplete li').text().search($(this).val());

            if (found < 0) {
              $(this).addClass('ui-autocomplete-nomatch').val('');
              $(this).delay(1500).removeClass('ui-autocomplete-nomatch', 500);
            }
          }
        }

CSS

.ui-autocomplete-nomatch { background: white url('../Images/AutocompleteError.gif') right center no-repeat; }
GordonB
@GordonB - do you have the images anywhere autocompleteerror.gif
ooo
it was just a 16x16 image that i drew.... not a library image.
GordonB
A: 

I'm doing it a little differently, caching the results and clearing the text field if the number of results for a certain term is zero:

<script type='text/javascript'>
function init_autocomplete( args )
{
     var resultCache = {};
     var currentRequestTerm = null;

     var closeCallback = function()
     {
         // Clear text field if current request has no results
         if( resultCache[currentRequestTerm].length == 0 )
             $(args.selector).val('');
     };

     var sourceCallback = function( request, responseCallback )
     {
         // Save request term
         currentRequestTerm = request.term;

         // Check for cache hit
         // ...
         // If no cache hit, fetch remote data
         $.post(
             dataSourceUrl,
             { ...  }, // post data
             function( response )
             {
                 // Store cache
                 resultCache[request.term] = response;

                 responseCallback( response );
             }
     };

     $(args.selector).autocomplete({
         close:  closeCallback,
         source: sourceCallback
     });
}
</script>
Sam
A: 

Hi,

This is kind of like the "SelectFirst". I'm trying to tie the text box and the search results list together. So, as they type something into the text box the results list selects the first item in the list. As the user narrows down the the list the selection would already be highlighted. Is this possible?

Thanks in advanced.

Rogeralan
@Rogeralan - This isn't an answer... post this as your own question and link to this one if you want. Even better, search. I'd bet this problem has been asked and answered already.
Stephen P