How to display images with typeahead.js?

落花浮王杯 提交于 2021-01-29 02:32:52

问题


How to display images with typeahead.js?

I am trying to display images for profiles into the list that is created by typehead.js.

At this moment I can only return texts, as you see there:

Here is what my javascript looks like:

And my PHP code, where I construct the profiles list:

The JSON $json_array_test looks like that:

Any idea how to achieve that?


回答1:


You can create template for suggestion:

$('#search-keywords-page').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'profiles',
  source: profiles,
  templates: {
      suggestion: Handlebars.compile('<div><img src="{{image}}" />{{title}} - {{nid}}</div>')
  }
});

Only return correct json from Your backend:

$array_test[$key] = array(
  'title' => $value->title,
  'nid' => $value->nid,
  'image' => $author_image_petite_url
);


来源:https://stackoverflow.com/questions/38348431/how-to-display-images-with-typeahead-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!