views:

2465

answers:

1

I am using the jquery autocomplete plugin, and am trying to trigger the autocomplete manually based on certain keys the user enters. I'm trying to bind the autocomplete to the input on the fly so the autocomplete can start looking for matches once a user enters a certain key to trigger "I'm now entering data to be searched on", which could be in the middle of the field, not necessarily at the beginning.

I'm binding a keypress event to an input. When the user enters a certain key, like "=", I want to display all of the elements in the array. When the user then enters a letter it would autocomplete the options that match from the array for that character on. This is more or less trying to mimic what happens in Excel, when a user hits the equals key and sees the functions available, except the "=" key does not have to be the first element in the input, it should autocomplete every time "=" is pressed and unbind that ac every time an option is selected.

var array1 = ['one','two','three'];

$.input1.bind('keypress', function(event) {
  var keyCode;
  if (event.keyCode > 0) {
    keyCode = event.keyCode;
  } else if (typeof(event.charCode) != "undefined") {
    keyCode = event.charCode;
  }

  if (String.fromCharCode(keyCode) == '=') {
    $.input1.unbind('keypress');  

    $.input1.autocomplete(array1);

    $.input1.blur();
    $.input1.focus();
    e = $.Event('keydown');
    e.which = keyCode;
    $.input1.trigger(e);
  }

});

Even if I get the autocomplete to trigger, if the user has entered anything before the text that might match, it wouldn't match because of the previous text. So, if the user enters "abd =", the autocomplete is getting "abb =" as its q parameter and not just "=".

Any help is really appreciated, im so stuck!!

+2  A: 

What you really need is to be able to filter the value of the field before the autocomplete plugin does the searching. Looking at the plugin it doesn't look like that is built in. BUT that doesn't stop us from adding it.

Open up the plugin code and find the function lastWord(), look at line 261. Then add this to that function:

if ( options.formatValue ){
    value = options.formatValue(value);
}

So the lastWord function should look like:

function lastWord(value) {
    if ( options.formatValue ){
        value = options.formatValue(value);
    }
    if ( !options.multiple )
        return value;
    var words = trimWords(value);
    return words[words.length - 1];
}

Then in your jquery code you can do something like this:

$(document).ready(function(){
    var array1 = ['one','two','three'];
    $('#test').autocomplete(array1,{
       formatValue: function(value){
        if (value.search('=') == -1) return '';
        return value.substring(value.search('=')+1);
       }
    });
});

This will let you filter and format the value of the field before the search. Since you only want to do the search if the value has the "=" character in it you search for it and if its not found you return an empty string. If its found you remove it and everything before it and return what the user is typing in after it.

PetersenDidIt
hey petersendidit,Thanks so much for the reply. After fumbling for 2 days I figured there had to be some kind of customization to the plugin itself. My requirements have actually gotten a little more tricky. A function has parameters, and each parameters are autocompletes to other arrays. So a function called "functionX" would have a form of "function()" that would show up with the "=" or a form of "[param]". When the user hits the "[" key, the autocomplete would then change to search on the array of that parameter. So if the user typed in "abc [" it would then be looking at the other one.
mrmcnerdypants
So each function has 2 forms, 1 form is like functionName() which all would show up if the user hits "=", and the other form which is just an enclosureStart char and enclosureEnd char. So when the user hits the "[" key in my example, no results should be coming back, but on the next key the user should see results from the function's parameter autocomplete. Does that make any sense? I can email a detailed use-case to you if I could make it any clearer... Again, thanks for much for the help!
mrmcnerdypants