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
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/