Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change jQuery autocomplete plugin default querystring key? (term to that i want)

jQuery autocomplete plugin sent request like this

mysite.com/suggestion?term=Sadegh

is there any way to change term querystring key to another? i can't find option that provide this for me ;)

like image 371
Sadegh Avatar asked Jun 28 '10 15:06

Sadegh


3 Answers

The above is not correct for the current release (1.8.6). Not sure if it ever was...

$(...).autocomplete({
  source: function(request, response) {
    $.getJSON("url", { foo: request.term }, response);
  }
});

foo: being the new param key name..

like image 81
lardawge Avatar answered Oct 08 '22 21:10

lardawge


I assume you're using jQuery UI AutoComplete

You need to provide a callback as the source, like this:

$(...).autocomplete({
    source: function(term, callback) {
        $.getJSON("url", { foo: term }, callback);
    }
});
like image 37
SLaks Avatar answered Oct 08 '22 20:10

SLaks


Actually I dug up the code that the plugin uses. Adapting it to change the term would be something like this:

$('#form').autocomplete({
    source: (function() {
        var xhr;
        return function(request, response) {
            if (xhr) {
                xhr.abort();
            }
            xhr = $.ajax({
                url: 'mysite.com/suggestion',
                data: {
                    foo: request.term
                },
                dataType: 'json',
                success: function(data) {
                    response(data);
                },
                error: function() {
                    response([]);
                }
            });
        }
    })()
});

I would say this has 2 advantages:

  1. Abort pending requests
  2. Call the response with an empty set in case of error, which seems more polite to me
like image 1
btx9000 Avatar answered Oct 08 '22 19:10

btx9000