E
E
entermix2015-05-24 23:22:32
JavaScript
entermix, 2015-05-24 23:22:32

Why is typeahead not working?

I use this plugin: https://github.com/bassjobsen/Bootstrap-3-Typeahead

This is how tooltips are:

$('#cityName').typeahead(
            {
                  source: [
                 {id: "0", name: "Москва"},
                 {id: "1", name: "Владивосток"}
                 ], 
                autoSelect: true
            });


And so no, why is that?
function getList(query) {
               return [
                        {id: "0", name: "Киев"},
                       {id: "1", name: "Чернигов"}
                   ]
        }

        $('#cityName').typeahead(
            {
                source: getList,
                autoSelect: true
            });


If you put alert in getList, it fires constantly when you enter text in input

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bkosun, 2015-05-25
@entermix

You need to run callback function
Example:

$('#cityName').typeahead(
            {
                source: function (query, handler) {
                           
                            var data = [
                                {id: "0", name: "Киев"},
                                {id: "1", name: "Чернигов"}
                             ]

                            handler(data);
                },
                autoSelect: true
            }
        );

V
Viktor Vsk, 2015-05-24
@viktorvsk

Why does the getList function need a query parameter that is not used?
source: getList means to pass a reference to a function, not to call a function (to get a result)
at all, the approach is kind of strange, but what you expect will probably be if you write source: getList()
And yes, do not use alerts .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question