dongqianzong4275 2015-03-23 18:58
浏览 81
已采纳

使用Laravel手动自动完成搜索,如何将信息传递给视图中的div

Im trying to make a autocomplete search, that searches from a string in a input, in my database from several tables, like Persons, Users, and Bands. Using Laravel 4.2.

I then want three objects to be accessed, and updated, in a #div on the view im already at.

I have tried alot, but this one was hard. My solution worked on localhost but not online, but it was a bad solution anyway, by passing a view with->(the objects). Failed online since headers already are sent.

I think my solution should be something like this:

View:

{{ Form::open(['action' => ['SearchController@search'], 'method' => 'post']) }}
{{ Form::text('term', '', ['class' => 'uk-width-1-1', 'id' =>  'term', 'placeholder' =>  'Søk etter person'])}}
{{ Form::close() }}

Controller:

   public function search(){
    $term = Input::get('term');

    $fest = Session::get('festival');
    $festival = Festival::find($fest);

        $persons = DB::table('fs_persons')
            ->join('fs_festival_persons', 'person_id','=','fs_persons.id')
            ->where('festival_id', '=', $fest)
            ->get();  

    foreach ($persons as $query)
    {
        if ((strpos(strtolower($query->firstname), strtolower($term)) !== false) OR ((strpos(strtolower($query->lastname), strtolower($term)) !== false))) {
           $results[] = [ 'id' => $query->id, 'value' => $query->firstname.' '.$query->lastname, 'tlf' => $query->tlf ];
        }

}
return Response::json($results);
}

But im unsure how to use it properly, here is just one object, tried making a multilevel array with objects, but it is all very confusing for me. This is alittle bit code from the jquery autocomplete, i get that working but I want my own presentation of the search results.

I would love to be able to do, in a view, that gets updated on keyup on the search input, and where i can use blade templating:

@if($persons)
 @if(sizeof($persons)>0)
    <div class="uk-width-small-4-4">
    Personar:
    </div>
    <div class="uk-width-small-4-4">
    @foreach($persons as $person)
    <?php
                $tmp_pers = Person::find($person->id);
?>

    <span class="search_name">
    <a href='{{ URL::to("festival/$festivalen->slug/person/$person->id") }}'>
    {{ $person->firstname }} {{ $person->lastname }}
    </a>
    </span>
</div>
@endforeach
@endif
@endif

But im unsure on how to makeetrieve variables out of json, all this was much harder than I expected, so hoping anyone could guide me, thanks!

  • 写回答

2条回答 默认 最新

  • donglu9134 2015-03-24 02:43
    关注

    There is a example project you can follow. laravel-smart-search

    also should exist bellow code set in your view and you can define values in "select:" which you want. when user select the any suggestion other element will change.

    View

    $(function() {
          $("#search_text").autocomplete({
              source: "{{URL::route('searchajax')}}",
              minLength: 2,
              select: function( event, ui ) {
                  $('#data').val(ui.item.id);
                  $('#search_text').val(ui.item.value);
                  $('#search').hide();
              }
          });
      });
    

    Controller

    foreach ( $persons as $query ){
            $data[] = array('value' => $row->$column, 'id' =>$row->id);
    }
    return Response::json($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容