简体   繁体   English

jQuery的自动完成与PHP foreach生成的结果

[英]jquery autocomplete with php foreach generated results

I have been trying for days to get jquery autocomplete to work the way I need it to. 我已经尝试了几天,以使jquery自动完成功能能够按我需要的方式工作。

so far I have this which works fine: 到目前为止,我的工作正常:

<script>
$(function() {
var availableTags = [<?php 

$taglist = Array();
  foreach ($users as $user) 
    if ($user->getAttribute('first_name') == ''){
       $taglist[] = '"'.$user->getUserName().'"';
}else{
       $taglist[] = '"'.$user->getAttribute('first_name').' '.$user->getAttribute('last_name').'"';
}

echo join(',', $taglist); 

?>];

$("#searchmem").autocomplete({
    source: availableTags,
    minLength: 2,
    select: function(event, ui) { 
        $("#searchmem").val(ui.item.label);
        $("#submit").click(); 
    }
}).data("autocomplete")._renderItem = function (ul, item) {
        return $("<li />")
        .data("item.autocomplete", item)
        .append("<a><img src='/files/avatars/1.jpg' />" + item.label + "</a>")
        .appendTo(ul);
    }
});
</script>

This will output the image /files/avatars/1.jpg next to the respective users username or full name. 这将在相应用户的用户名或全名旁边输出图像/files/avatars/1.jpg。

The problem I'm having is trying to output the right users avatar. 我遇到的问题是尝试输出正确的用户头像。 Each .jpg file corresponds with the username so I could have used $user->getUserID() in the src but this won't work because it's not inside the foreach $users as $user loop. 每个.jpg文件都与用户名相对应,因此我可以在src中使用$ user-> getUserID(),但这将无法正常工作,因为它不在foreach $ users中作为$ user循环。

I have tried putting the whole autocomplete script inside the foreach which when tested did alert the right thing but autocomplete wouldn't work. 我试过将整个自动完成脚本放入foreach中,该脚本在测试时确实会警告正确的事情,但自动完成将不起作用。

I have also tried creating two variables such as 我也尝试过创建两个变量,例如

availableTags1 = { label: .... $user->getUserName() etc... } availableTags1 = {标签:...。$ user-> getUserName()等...}

availableTags2 = { avatar: .... $user->getUserID() etc... } availableTags2 = {头像:...。$ user-> getUserID()等...}

availableTags = availableTags1 + availableTags2; availableTags = availableTags1 + availableTags2;

and

.data("autocomplete")._renderItem = function (ul, item) {
    return $("<li />")
    .data("item.autocomplete", item)
    .append("<a><img src=' + item.avatar + ' />" + item.label + "</a>")
    .appendTo(ul);
}

But again this didn't work. 但这又没有用。 I'm completely lost! 我完全迷路了! How can I get it to output the image alongside the relevant username? 如何获得将图像与相关用户名一起输出的信息? Help would be much appreciated. 帮助将不胜感激。

In your case, you have to build an array like : 在您的情况下,您必须构建一个像这样的数组:

var availableTags = [
    {label: '...', avatar: '...'},
    {label: '...', avatar: '...'},
    ...
];

And use: 并使用:

.data("autocomplete")._renderItem = function (ul, item) {
    return $("<li />")
    .data("item.autocomplete", item)
    .append("<a><img src='" + item.avatar + "' />" + item.label + "</a>") // Note the additional double quotes
    .appendTo(ul);
}

(see this example on jQuery UI for using custom data) (有关使用自定义数据的信息,请参见jQuery UI上的此示例)

Then, for generating you array via PHP, you should use: 然后,为了通过PHP生成数组,应使用:

<?php
$taglist = Array();
foreach ($users as $user) {
    $data = array();
    if ($user->getAttribute('first_name') == ''){
        $data["label"] = $user->getUserName();
    } else {
        $data["label"] = $user->getAttribute('first_name').' '.$user->getAttribute('last_name');
    }
    $data["avatar"] = $user->getUserID();
    $taglist[] = $data;
}
?>
var availableTags = <?php echo json_encode($taglist); ?>;

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

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