Adding Label, value and picture in Bootstrap's Typeahead

后端 未结 5 996
误落风尘
误落风尘 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:37

    typeahead.initialize();
    var typeahead = {
    
        typeaheadInit: function() {
            var jsonData = [{
                'id': 1,
                'name': 'Parajanov Museum',
                'image': 'img/1.png'
            }, {
                'id': 2,
                'name': 'Parajanov’s Movie',
                'image': 'img/2.png'
            }, {
                'id': 3,
                'name': 'S Parajanov’s about his series of Giocondas',
                'image': 'img/3.png'
            }, {
                'id': 4,
                'name': 'S Parajanov’s about the colore of pomegranate',
                'image': 'img/4.png'
            }, {
                'id': 5,
                'name': 'George Michael',
                'image': 'img/5.png'
            }];
    
            var productNames = [];
            $.each(jsonData, function(index, product) {
                productNames.push(product.name + "#" + product.image + "#" + product.id);
            });
            $('#typeahead').typeahead({
                source: productNames,
                highlighter: function(item) {
                    var parts = item.split('#'),
                        html = '
    '; html += '
    '; html += '
    '; html += '

    ' + parts[0] + '

    '; html += '
    '; html += '
    '; var query = this.query; var reEscQuery = query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); var reQuery = new RegExp('(' + reEscQuery + ')', "gi"); var jElem = $(html); var textNodes = $(jElem.find('*')).add(jElem).contents().filter(function() { return this.nodeType === 3; }); textNodes.replaceWith(function() { return $(this).text().replace(reQuery, '$1'); }); return jElem.html(); }, updater: function(selectedName) { var name = selectedName.split('#')[0]; return name; } }); }, initialize: function() { var _this = this; _this.typeaheadInit(); } };

    Source: https://jsfiddle.net/geghamk/59eranrc/3/

提交回复
热议问题