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条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。