How to remotely load options in auto complete using Typeahead?

Asked

Viewed 314 times

0

I was studying the typeahead.js with bootstrap 3, and I’m trying to make an auto-complete request to the server to bring the list options.

This is the plugin I’m using: Github Bootstrap 3 Typeahead

This is the function I use to create auto complete :

$(function(){
        var options = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: 'loadNameChoices',
                replace: function(url, uriEncodedQuery) {
                      return url + '?input=' + uriEncodedQuery
                },
            }
        });

        options.initialize();

        $('#nome').typeahead({source:options.ttAdapter()});
    })

When typing a letter in the field, the request is made on the server, which returns a with names. But autocomplete does not work.

What is the right way to implement this remote autocomplete?

My back-end is done with . And in front-end use bootstrap3 + .

This is the service that returns the with the names. This service works correctly.

def loadNameChoices(){
    render(contentType: "application/json") {
        service.loadNames(params['input'])
    }
}

If I just do:

$('#nome').typeahead({
   source: ['TESTE','TESTE']
});

Autocomplete works. I think there’s a problem with Bloodhound

  • 1

    Which programming language you use?

  • My backend is grails. Front bootstrap3 + jquery. I added the controller code that receives the request.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.