Adding Label, value and picture in Bootstrap's Typeahead

后端 未结 5 1061
误落风尘
误落风尘 2021-02-02 01:59

I am playing around with Typeahead and I am trying to figure out if there is a way to display Pictures and Labels in the search query as well? Something like how Twitter does wh

5条回答
  •  轮回少年
    2021-02-02 02:42

    highlighter is not working anymore.

    Use templates, example:

    var my_friends = [
         {name: "John", picture: "http://..."}
        ,{name: "Mel", picture: "http://..."}
    ];
    
    var friends = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      local: my_friends
    });
    friends.initialize();
    $('#friend_name').typeahead(
        {
            hint: true,
            highlight: true,
            minLength: 1,
        },
        {
            name: 'friends',
            displayKey: 'name',
            source: friends.ttAdapter(),
            templates: {
                empty: 'not found', //optional
                suggestion: function(el){return ''+el.name}
            }
        }
    );
    

    Source: https://gist.github.com/jharding/9458780#file-custom-templates-js

提交回复
热议问题