Can the default "Term" name passed in the "jquery UI autocomplete" feature be changed?
Asked Answered
D

2

5

I am trying to change the "term" field that is set to that by default with the jquery ui autocomplete feature. Is it possibly to easily change it to "q" (query) without going and changing it in the "core" file?

JavaScript:

<script>
    $(function() {
        $( "#spotify_song_search" ).autocomplete({
            source: "http://ws.spotify.com/search/1/track.json",
            data: { 
                q: request.term 
            },
            dataType: "getjson",
            minLength: 3,
            select: function( event, ui ) { 
                alert('select'); 
            }
        });
    });
</script> 
Dabbs answered 29/5, 2012 at 1:3 Comment(0)
K
17

Yes, it's possible by making your own AJAX request.

Assume you have the following setup:

$("#myfield").autocomplete({
    source: '/my_url/myservice.xyz'
});

Autocomplete by default (as you noticed) sends requests that look like:

myservice.xyz?term=abc"

You can supply a function reference to the source option of autocomplete. Inside that function you can make your own AJAX request, which would look like this:

$("#myfield").autocomplete({
     source: function (request, response) {
         // request.term is the term searched for.
         // response is the callback function you must call to update the autocomplete's 
         // suggestion list.
         $.ajax({
             url: "/my_url/myservice.xyz",
             data: { q: request.term },
             dataType: "json",
             success: response,
             error: function () {
                 response([]);
             }
         });
     });
});

This should generate a request looking more like:

myservice.xyz?q=abc
Kamchatka answered 29/5, 2012 at 1:29 Comment(6)
Cleaner to use getJSON as in the example I linked to, IMO.Squib
@DaveNewton: Yeah you're right. I only used the long form of $.ajax because that's what the plugin does internally. But getJSON would be (nearly) equivalent and cleaner. Additionally, with $.ajax you can make sure an empty list is sent to the widget on error, which you cannot do with getJSON since it does not allow you to specify an error callback.Kamchatka
Sure you can, using the .error method, although it doesn't fit into the autocompleter as nicely--but IMO you'd want to do something more drastic than an empty list if you couldn't complete the request anyway.Squib
I get an error saying that request is not defined, I'll add my code to the question.Dabbs
Look closely at your code: you are missing the function definitionKamchatka
Excellent - for this solution :)Carrycarryall
S
1

You could use the callback source option and make your own request.

http://jqueryui.com/demos/autocomplete/

Squib answered 29/5, 2012 at 1:12 Comment(2)
I'm sorry I don't follow. What do you mean "make your own request"? Aren't the source values searchable queries?Dabbs
@PeteA ... Did you read about the callback source option? There's also an example in the multiple remote example.Squib

© 2022 - 2024 — McMap. All rights reserved.