Bootstrap Typeahead not showing hints as expected

后端 未结 3 2002
青春惊慌失措
青春惊慌失措 2020-12-10 07:18

I\'m using Typeahead to show hints from an items database and a stores database. When I show hints only from items it shows fine, when I show only from stores works fine too

相关标签:
3条回答
  • 2020-12-10 07:45

    Found the problem. It's a bug in typeahead.bundle.js (v 0.11.1). It's counting the number of rendered hints before appending them, so if the number of hints equals the limit it'll append an empty array.

    To prevent this I just switched lines 1723 and 1724 so it looks like this:

    that._append(query, suggestions.slice(0, that.limit - rendered));
    rendered += suggestions.length;
    

    Already reported the issue in typeahead's github.

    0 讨论(0)
  • 2020-12-10 07:55

    @Luciano Garcia Bes - to complete your anwser, below I've post all changes which are needed : you have rigth to switch those lines, but I need remove - rendered too. So finally It sholud looks like this (whole function):

                function async(suggestions) {
                    suggestions = suggestions || [];
                    if (!canceled && rendered < that.limit) {
                        that.cancel = $.noop;
                        that._append(query, suggestions.slice(0, that.limit));
                        rendered += suggestions.length;
                        that.async && that.trigger("asyncReceived", query);
                    }
                }
    

    more about this iisue : https://github.com/twitter/typeahead.js/issues/1415

    0 讨论(0)
  • 2020-12-10 08:01

    Another option to work around the issue if you are using a hosted CDN version is to set limit:'Infinity' when initiating:

    $(".input-search").typeahead({
            hint: true,
            highlight: true,
            minLength: 2,
        }, {
            limit:'Infinity',
            source: engine,           
        });
    

    and then limit the results on the server

    0 讨论(0)
提交回复
热议问题