views:

202

answers:

1

I'm using Jörn Zaefferers autocomplete plugin for jQuery to enable a live search field, which is working great. The user input gets sent to a search service, that returns formatted results in Json format. When a result gets chosen, the values are entered into a separate table.

What I'm having trouble with is: After a user selects a search result from the drop-down, I don't want the search (parameters entered by the user) to be overwritten by the result. Which is the default behaviour.

I'm looking for an option that I can use to disable this, If it doen't exist though, I may have to look for a workaround / different plugin.

Here is my current usage of the autocomplete plugin with options:

$('#searchInput').autocomplete('searchUrl',
    {
        dataType: 'json',
        parse: function(data) {
            //logic to parse search results that are returned from search
            return datarows;
        },
        formatItem: function(row, i, max) {
            return row.Description;
        },
        width: 500,
        highlight: false,
        multiple: false,
        minChars: 2,
        delay: 800,
        selectFirst: false,
        autoFill: false,
        cacheLength: 10 
    })
    .result(function(event, item) {
        //logic to handle item chosen by the user
    });
+2  A: 

What you want is not an autocomplete function, therefore an autocomplete plugin won't work. You have to modify the plugin so that it doesn't overwrite the result. I'm looking at it to see what changes you need to make and will add my proposed solution shortly.


Something came up at work, so I have to make this quick.

In the plugin, the input value is modified via the $input.val() function. When an argument is passed to it, like this:

$input.val('test')

The input's contents change, in this case to "test". You have to get rid of those lines. Note that calling $input.val() only returns the value without changing it so you want to leave those lines alone.

A couple I found:

On line 233, comment

$input.val(v);

On line 297, comment

$input.val($input.val() + sValue.substring(lastWord(previousValue).length));

There are more for sure. From what I have seen I would expect to find at least one in the function that hides the menu. Use the editor's search function.

This may or may not break the plugin, you'll have to start by commenting out those lines and then debug it.

I hope I was helpful enough, good luck :)

Kaze no Koe
Ah, of course..for some reason the thought didn't occur to me that I could modify the plugin itself. I'll have a look into this in a bit and let you know how it works out
pavsaund
Works great! As you say I'm working against the way the autocomplete-plugin works out of the box, and seeing that there aren't any options that do this modifying the plugin is the way to go. Thx :)
pavsaund
Great, an additional tip: now that you know which lines can be safely removed, exploit JS's dynamic nature to make the plugin more generic. Add an initialization option called "pleaseDontChangeTheInputFieldValueThanks" and instead of commenting out the offending lines wrap them in IFs that check for the truth value of the option. That way you can reuse the plugin if you need a traditional autocomplete function. Good luck with the rest of your project :)
Kaze no Koe