0

I think I have around 5 hours since I'm struggling with the typeahead.js library. I'm trying to load the data using the remote functionality but I don't have any luck on making it working completely.

These are the js libraries that I'm loading:

<script src="/Scripts/jquery-2.1.4.js"></script>
<script src="/Scripts/handlebars-v3.0.3.js"></script>
<script src="/Scripts/typeahead.bundle.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>

Then, in order to initialize my typeahead feature, I'm using this piece of code:

$(function () {
    var cards = new Bloodhound({
        datumTokenizer: function (datum) {
            console.log(datum);
            return Bloodhound.tokenizers.whitespace(datum.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/home/suggest?searchQuery=%QUERY',
            wildcard: '%QUERY',
            filter: function (response) {
                return $.map(response, function (item) {
                    return {
                        value: item.Text,
                        tokens: item.Document
                    }
                });
            }
        }
    });

    cards.initialize();

    $(".typeahead").typeahead({
        hint: true,
        highlight: true,
        minLength: 3
    },
    {
        name: 'CardSearch',
        displayKey: 'value',
        valueKey: 'value',
        source: cards.ttAdapter()
    }).on('typeahead:render', function(ev, suggestion, isAsync, dsName) {
        console.log(suggestion, isAsync, dsName);
    }).on('typeahead:asyncreceive', function(ev, query, dsName) {
        console.log(query, dsName);
    });
});

From my debugging steps I realize that the remote data retrieval is working perfectly because:

a) in firefox / firebug I see the request and the response

b) I've put a console.log statement inside filter assigned function and everything is fine.

... And everything stops here... Nothing is being displayed as a dropdown list and also the hint is not working either...

What I've done next is to put some console.log statements on typeahead:render and typeahead:asyncreceive events and what I see is that all the parameters (except the jquery event) are undefined, which makes me believe that somehow, the typeahead is not being initialized correctly.

Please, if you have any idea or suggestions, give me a hint.

THANK YOU!

Later edit: This is how the generated HTML looks like:

<div class="col-sm-3 col-md-3 pull-right">
<form action="/Home/Search" class="navbar-form" method="get" role="search">
<div class="input-group">
  <input class="form-control typeahead" id="query" name="query" placeholder="Search" type="text" value="" />
    <div class="input-group-btn">
      <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
    </div>
</div>
</form>                

Edi
  • 660
  • 9
  • 22
  • http://stackoverflow.com/questions/19538591/jquery-autocomplete-twitter-typeahead-populate-multiple-fields this of any use – pee2pee Jun 10 '15 at 13:12
  • No, because I'm not getting to the point of having something displayed... I see that also in the issue the data was local... – Edi Jun 10 '15 at 13:18
  • What's the HTML that sits behind this? – pee2pee Jun 10 '15 at 13:24
  • I updated the question also with the HTML. Thank you! – Edi Jun 10 '15 at 13:32
  • can you please create a demo on http://jsfiddle.net? – Dhiraj Jun 10 '15 at 17:10
  • This is what I'm trying to do but I have no idea how can I simulate the `remote` scenario so I can get the JSON back from the same origin to have a 100% identical scenario. – Edi Jun 10 '15 at 20:11
  • Unfortunatelly for now I dropped this functionality to a more simpler one - instead of retrieving a complete set of data, I got from the server a list of strings (json) and now it's working. Anyway, if you take a look in the page source + js from here `http://twitter.github.io/typeahead.js/`, you will see that the things are a bit more complicated than described in the docs. When I will have a bit of time, I will try to make this working also and come back with a complete solution. – Edi Jun 11 '15 at 09:01

0 Answers0