2017-08-13 12 views
1

私は完全に動作するJQuery UIオートコンプリートを使用したスクリプトを用意しています。ユーザーのfirsnameとlastnameを表示する検索プロセスがあります。しかし私のdatabseでは、ユーザーの写真もあり、私はfristnameとlastnameの提案にそれを表示したい。イメージをJQueryで表示するuiオートコンプリート

(データベースに、PICは画像のURLを含む)

スクリプト:

$(function() {  
 
    $("#search").autocomplete({ 
 
     source: "autocomplete.php", 
 
     minLength: 1, 
 
     select: function(event, ui) { 
 
      var url = ui.item.id; 
 
      if(url != '') { 
 
       location.href = '...' + url; 
 
      } 
 
     }, 
 
     html: true,   
 
     open: function(event, ui) { 
 
      $(".ui-autocomplete").css("z-index", 1000); \t \t \t 
 
     } 
 
    });  \t 
 
});

autocomplete.php

<?php  
 
if (!isset($_REQUEST['term'])) { 
 
\t exit; 
 
} 
 
    
 
$mysqli = new MySQLi($DB_host,$DB_login,$DB_pass,$DB_select); 
 
     
 
$term = trim(strip_tags($_GET['term'])); 
 
$term = preg_replace('/\s+/', ' ', $term); 
 
    
 
$a_json = array(); 
 
$a_json_row = array(); 
 
    
 
$a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted...")); 
 
$json_invalid = json_encode($a_json_invalid); 
 
    
 
if(preg_match("/[^\040\pL\pN_-]/u", $term)) { 
 
    print $json_invalid; 
 
    exit; 
 
} 
 

 
if ($data = $mysqli->query("SELECT * FROM users WHERE firstname LIKE '%$term%' OR lastname LIKE '%$term%' ORDER BY firstname , lastname")) { 
 
\t while($row = mysqli_fetch_array($data)) { 
 
\t \t $firstname = htmlentities(stripslashes($row['firstname'])); 
 
\t \t $lastname = htmlentities(stripslashes($row['lastname'])); 
 
\t \t $id= htmlentities(stripslashes($row['id'])); 
 
\t \t $pic= htmlentities(stripslashes($row['pic'])); 
 
\t \t $a_json_row["id"] = $id; 
 
\t \t $a_json_row["value"] = $firstname.' '.$lastname; 
 
\t \t $a_json_row["label"] = $firstname.' '.$lastname; 
 
\t \t array_push($a_json, $a_json_row); 
 
\t } 
 
} 
 
    
 
/* jQuery wants JSON data */ 
 
echo json_encode($a_json); 
 
flush(); 
 
?>

助けてください。

答えて

1

あなただけsrc属性にURLを渡し、選択に続いて
... IMGのURLを受信する準備ができ<img>要素を持っている必要があります。以下のデモで

、私はあなたのJSONレスポンスをシミュレート....
は「ジョン・ドウ」または「システム管理者」を試してみてください。

$(function() { 
 
    $("#search").autocomplete({ 
 
    source: //"autocomplete.php", 
 
    [ 
 
     { 
 
     id:"John Doe", 
 
     value:"John Doe", 
 
     label:"John Doe", 
 
     img:"http://images.maskworld.com/is/image/maskworld/bigview/john-doe-foam-latex-mask--mw-117345-1.jpg" 
 
     }, 
 
     { 
 
     id:"System Admin", 
 
     value:"system Admin", 
 
     label:"system Admin", 
 
     img:"http://www.ericom.com/imgs/braftonArticles/3-things-every-system-admin-should-know-about-virtualization_16001411_800906167_0_14057272_500.jpg" 
 
     } 
 
    ], 
 
    minLength: 1, 
 
    select: function(event, ui) { 
 
     /* 
 
     var url = ui.item.id; 
 
     if(url != '') { 
 
     location.href = '...' + url; 
 
     } 
 
     */ 
 
     var img = ui.item.img; 
 
     $("#pic").attr("src",img); 
 
    }, 
 
    html: true, 
 
    open: function(event, ui) { 
 
     $(".ui-autocomplete").css("z-index", 1000); 
 
    } 
 
    }); 
 
});
img{ 
 
    max-height:350px; 
 
    max-width:200px; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 

 
<input type="text" id="search"><br> 
 
<img id="pic">


EDIT

はたぶん、あなたは私がjQuery-UI documentationでこれを見つけた
...ドロップダウンにイメージを持っていることを望みます。

$(function() { 
 

 
     $("#search").autocomplete({ 
 
     source: //"autocomplete.php", 
 
     [ 
 
      {id:"John Doe", 
 
      value:"John Doe", 
 
      label:"John Doe", 
 
      img:"http://images.maskworld.com/is/image/maskworld/bigview/john-doe-foam-latex-mask--mw-117345-1.jpg"}, 
 
      {id:"system Admin", 
 
      value:"system Admin", 
 
      label:"system Admin", 
 
      img:"http://www.ericom.com/imgs/braftonArticles/3-things-every-system-admin-should-know-about-virtualization_16001411_800906167_0_14057272_500.jpg"} 
 
     ], 
 
     minLength: 1, 
 
     select: function(event, ui) { 
 
      /* 
 
      var url = ui.item.id; 
 
      if(url != '') { 
 
      location.href = '...' + url; 
 
      } 
 
      */ 
 
     }, 
 
     html: true, 
 
     open: function(event, ui) { 
 
      $(".ui-autocomplete").css("z-index", 1000); 
 

 
     } 
 
     }) 
 
     .autocomplete("instance")._renderItem = function(ul, item) { 
 
     return $("<li><div><img src='"+item.img+"'><span>"+item.value+"</span></div></li>").appendTo(ul); 
 
     }; 
 

 
    });
.ui-menu img{ 
 
    width:40px; 
 
    height:40px; 
 
} 
 
.ui-menu li span{ 
 
    font-size:2em; 
 
    padding:0 0 10px 10px; 
 
    margin:0 0 10px 0 !important; 
 
    white-space:nowrap; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 

 
<input type="text" id="search"><br>

+0

あなたの答えをありがとう、私は、データベースから写真を撮りたいです。各ユーザは異なるプロフィール画像を有する。だから私は、プロファイルの写真を表示し、次に名字と姓を表示することを提案したい。 – Dilak

+0

私の例では、autocompleteによって要求されたjsonからの 'img'ソース情報を取得します。だからあなたのデータベースから。 –

+0

こういうことをするべきですか?$ a_json_row ["img"] = $ pic; autocomplete.phpで? – Dilak

1

あなたは_renderItem(ul, item)を使用することができます。

$("#search").autocomplete({ 
 
    source: [ 
 
     { 
 
      value: "aaa", 
 
      label: "aaa", 
 
      desc: "aaa", 
 
      icon: "https://dummyimage.com/50x50/000/fff&text=aaa" 
 
     }, 
 
     { 
 
      value: "bbb", 
 
      label: "bbb", 
 
      desc: "bbb", 
 
      icon: "https://dummyimage.com/50x50/000/fff&text=bbb" 
 
     }, 
 
     { 
 
      value: "ccc", 
 
      label: "ccc", 
 
      desc: "ccc", 
 
      icon: "https://dummyimage.com/50x50/000/fff&text=ccc" 
 
     } 
 
    ], 
 
    minLength: 0, 
 
    select: function (event, ui) { 
 
     $("#search").val(ui.item.label); 
 
     return false; 
 
    } 
 
}); 
 
$("#search").data("ui-autocomplete")._renderItem = function (ul, item) { 
 
    return $('<li/>', {'data-value': item.label}).append($('<a/>', {href: "#"}) 
 
      .append($('<img/>', {src: item.icon, alt: item.label})).append(item.label)) 
 
      .appendTo(ul); 
 
};
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> 
 

 

 
<input type="text" id="search">

+0

あなたの答えをありがとうが、私はデータベースから写真を撮りたい。各ユーザは異なるプロフィール画像を有する。だから私は、プロファイルの写真を表示し、次に名字と姓を表示することを提案したい。 – Dilak

+0

phpファイルはautocomplete.phpです。 – Dilak

関連する問題