【问题标题】:jQuery-autocomplete add images in resultjQuery-autocomplete 在结果中添加图像
【发布时间】:2017-02-19 05:04:51
【问题描述】:

我有一个项目,用户可以从搜索栏中搜索其他用户,但使用自动完成,我只能获取其他用户的用户名,而不是个人资料图片。我的users 表有一列avatar,其中存储了每个用户的个人资料图片。
控制器:

public function autocomplete1() {
    $queries = User::where(function($query) {
        $term = Input::get('term');
        $query->where('username', 'like', '%'.$term.'%');
    })->take(6)->get();
    $results = array();

    foreach($queries as $query) {
        $results[] = ['query' => $query->avatar, 'avatar' => $query->username];
    }
    return response::json($results);    
}

查看:

$(function() {
    $('#text3').autocomplete({
        source: "autocomplete1",
        minlenght: 2,
        autoFocus: true,
        select: function(event, ui) {
            $('#text3').val(ui.item.value);
        }
    });
    $('#text3').data("ui-autocomplete")._renderItem = function(ui, item) {
        var $li = $("<li style='width:800px; margin-left:10px; margin-bottom:5px'>"),
        $img = $("<img style='width:8%'>");

        $img.attr({
            src: '{{URL::to('/')}}/images/' + item.avatar,
            alt: item.value
        });
        $li.attr('data-value', item.value);
        $li.append("");
        $li.append($img).append("" + item.value);
        return $li.appendTo(ul);    
    };
});

【问题讨论】:

  • 精氨酸!复制粘贴您的代码并正确格式化,不要使用屏幕截图:-((
  • 更新了问题@AlFoиceѫ

标签: php jquery laravel autocomplete


【解决方案1】:

在您的 php 代码中,您似乎只是从每个对象 ($v-&gt;username) 中提取了 username。试试:

$results[]=['value'=>$v];

我认为这会将整个对象存储在数组中(希望带有您想要的个人资料图片字段)。

【讨论】:

  • 我认为拉所有东西都行不通,因为我的桌子大约有 10 个 cloumns
  • 如果您真的不想要额外的数据字段(或它们包含敏感信息),您可以像完成用户名的方式一样提取个人资料图片字段。 $v->profile_pic(如果 profile_pic 是数据对象中的属性名称)
  • 我厌倦了它,但它给了我该图像的名称而不是显示图像
  • 不,它只是返回了图像的名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
相关资源
最近更新 更多