87

I need a little bit help with JQuery UI Autocomplete. I want my textfield (.suggest-user) display names from an AJAX request. This is what I have:

jQuery("input.suggest-user").autocomplete({
    source : function(request, response) {
        var name = jQuery("input.suggest-user").val();
        jQuery.get("usernames.action?query=" + name, function(data) {
            console.log(data);  // Ok, I get the data. Data looks like that:
            test = data;        // ["one@abc.de", "onf@abc.de","ong@abc.de"]
            return test;        // But what now? How do I display my data?
        });
    },
    minLength : 3
});

Any help is very much appreciated.

Salman A
  • 262,204
  • 82
  • 430
  • 521
Bernie
  • 1,607
  • 1
  • 18
  • 30

8 Answers8

165

Inside your AJAX callback you need to call the response function; passing the array that contains items to display.

jQuery("input.suggest-user").autocomplete({
    source: function (request, response) {
        jQuery.get("usernames.action", {
            query: request.term
        }, function (data) {
            // assuming data is a JavaScript array such as
            // ["one@abc.de", "onf@abc.de","ong@abc.de"]
            // and not a string
            response(data);
        });
    },
    minLength: 3
});

If the response JSON does not match the formats accepted by jQuery UI autocomplete then you must transform the result inside the AJAX callback before passing it to the response callback. See this question and the accepted answer.

Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 2
    It is a little bit more simple. jQuery autocomplete supports a get url for the 'source' parameter. `source: "usernames.action" `. Your method should return a json array and should accept a parameter called "term". – Eli Jun 17 '14 at 09:19
  • @Elisa: correct. The OP used a callback in the example (may be for a reason) so I followed suit. – Salman A Jun 17 '14 at 19:51
  • 1
    This doesn't work for me... it returns all of the items in the list, instead of the ones that match the query term. – geoidesic Jan 27 '15 at 14:02
  • OP filters the results on server side, this answer assumes that. – Salman A Jan 27 '15 at 15:16
  • Is there a way to filter results in the front end, with the jquery autocomplete library, after doing an ajax call? – Timothy Apr 06 '15 at 18:43
  • @TechyTimo you can filter the results inside `jQuery.get` callback before calling the `response` function. In the above example the code would go where the comments are. – Salman A Apr 06 '15 at 21:43
  • Hello @SalmanA, can you check this once pls http://stackoverflow.com/questions/33410824/autocomplete-in-jquery-with-dynamically-added-elements Thanks. – Jaikrat Oct 29 '15 at 11:47
  • `response()` function inside ajax call will create some `
  • ` elements to display. How shall I do if I want to add an attribute to that `
  • ` when it's created?
  • – SagittariusA Mar 10 '16 at 11:26
  • 1
    @LoryLory if you want to modify the autocomplete's suggestion list then search stackoverflow for "jquery ui autocomplete _renderItem". – Salman A Mar 10 '16 at 17:35
  • What does "query: request.term" means? – Sasa1234 Sep 23 '17 at 07:06
  • @Bernie What does "query: request.term" means? – Sasa1234 Sep 23 '17 at 07:06
  • @Sasa1234 - "query: request.term" is what is send to the server as a query string. In this example if user enters FOO then request.term=foo and server request will be send to usernames.action?query=foo. – Ross Feb 12 '18 at 21:45