简体   繁体   English

如何将自定义图像添加到预先输入下拉菜单?

[英]How can I add custom image to typeahead drop-down menu?

The drop-down is populated from a remote table which also contains the url of the corresponding image. 下拉列表从远程表中填充,该表还包含相应图像的URL。 I am using typeahead 0.9.3. 我正在使用typeahead 0.9.3。 What method can be used to add custom images to the autocomplete? 可以使用哪种方法将自定义图像添加到自动完成?

<?php

$dbh = new PDO('mysql:host=localhost;dbname=****', 'root', '*****');

$query ='%'.$_GET['query'].'%';

$stmt = $dbh->prepare("SELECT event FROM events WHERE event LIKE :query");

$stmt->bindParam(':query', $query, PDO::PARAM_STR);
$stmt->execute();

$results = array();

foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $row) {
    $results[] = $row;
}

echo json_encode($results);

?>

You have to add a template option to your typeahead initialization. 您必须为您的预先初始化添加template选项。 You template must use hogan as rendering engine to replace the img source for your remote value. 您的模板必须使用hogan作为渲染引擎来替换远程值的img源。

For example, if you remote query returns a JSON object like this: 例如,如果远程查询返回如下的JSON对象:

{"value":"test","img":"url to image"}

You must to configure typeahead this way: 您必须以这种方式配置typeahead:

$input.typeahead({
   name: "autocomplete",
   template: "{{value}}<img src='{{img}}'/>",
   engine: Hogan,
   remote: "/remoteUrlToFill?q=%QUERY"
});

I've created a straightforward jsfiddle to test this (see at the end of the javascript area to see the custom code): 我已经创建了一个简单的jsfiddle来测试它(请参阅javascript区域的末尾以查看自定义代码):

http://jsfiddle.net/vfportero/KJFje/1/ http://jsfiddle.net/vfportero/KJFje/1/

Instead of remote source I used a local json object but this mustn't be a problem if your remote response looks like the local object 我使用本地json对象而不是远程源,但如果您的远程响应看起来像本地对象,则这不应该是一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM