Typeahead.js matches only the beginning of a word

My Typeahead Code:

var events = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        limit: 100,
        prefetch: {
            url: Ajax.pluginurl + 'json/events.json',
            ttl: 1
        }
    });

    events.initialize();

    initialize_events_typeahead ();

    function initialize_events_typeahead () {
        $('.event_name').typeahead(null, {
            name: 'event',
            displayKey: 'name',
            source: events.ttAdapter(),
            templates: {
                empty: [
                    '<div class="empty-message">',
                    Ajax.no_results_found,
                    '</div>'
                ].join('\n'),
                suggestion: function(data){
                    return '<p><strong>' + data.name + '</strong> - ' + data.description + '</p>';
                }
            },
            engine: Hogan
        });


        $('.event_name').on("typeahead:selected typeahead:autocompleted", function(e,datum) {
            $(this).parent().parent().parent().parent().find('.event_description').val(datum.description);
        });
    }

For some reason, this code only matches the beginning of the name. However, in the demo section http://twitter.imtqy.com/typeahead.js/examples/, the prefetch section has the same code as I see it, even coinciding with the beginning of entering something in the middle of the world.

How can i fix this? Thanks

EDIT: JSON:

[{"name":"P\u0159\u00edjezd host\u016f","description":"Cras ullamcorper ornare semper. Phasellus faucibus augue congue dapibus mollis. "},{"name":"Ob\u0159ad","description":"Curabitur fermentum diam quis viverra sodales. Phasellus sed sollicitudin magna, a dictum metus."},{"name":"Ve\u010dern\u00ed z\u00e1bava","description":"Curabitur fermentum diam quis viverra sodales."},{"name":"After-party","description":"Proin ipsum odio, vehicula vel diam a, dapibus suscipit velit. "},{"name":"Sn\u00eddan\u011b","description":"Proin ornare tempus ipsum at blandit. Nam hendrerit dolor et interdum vulputate."}]
+4
source share
2 answers

Not sure why their samples show a match for the middle words, but I guessed you should develop your own matching algorithm ( datumTokenizer ).

(, Bloodhound):

var engine = new Bloodhound({

    datumTokenizer: function(d){
        var tokens = [];
        //the available string is 'name' in your datum
        var stringSize = d.name.length;
        //multiple combinations for every available size
        //(eg. dog = d, o, g, do, og, dog)
        for (var size = 1; size <= stringSize; size++){          
          for (var i = 0; i+size<= stringSize; i++){
              tokens.push(d.name.substr(i, size));
          }
        }

        return tokens;
    },...
+6

, , , , , :

<

, , , JavaScript Unicode 6.3:

https://github.com/walling/unorm

Bower.

0

Source: https://habr.com/ru/post/1539123/


All Articles