使用 Laravel 手动自动完成搜索,如何将信息传递给视图中的 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29218219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manual autocomplete search with Laravel, how to pass on info to div's in views
提问by Terje Nesthus
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.
我试图进行自动完成搜索,从输入中的字符串中搜索,在我的数据库中从几个表中搜索,如人员、用户和乐队。使用 Laravel 4.2。
I then want three objects to be accessed, and updated, in a #div on the view im already at.
然后,我希望在视图中的 #div 中访问和更新三个对象。
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.
但是我不确定如何正确使用它,这里只有一个对象,尝试使用对象创建多级数组,但这对我来说非常混乱。这是 jquery 自动完成中的一点代码,我得到了它的工作,但我想要我自己的搜索结果演示。
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:
在一个视图中,我希望能够在搜索输入上的 keyup 上进行更新,以及我可以在哪里使用刀片模板:
@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 make\retrieve variables out of json, all this was much harder than I expected, so hoping anyone could guide me, thanks!
但是我不确定如何从 json 中创建\检索变量,所有这些都比我预期的要困难得多,所以希望有人能指导我,谢谢!
采纳答案by namal
There is a example project you can follow. laravel-smart-search
您可以遵循一个示例项目。 laravel 智能搜索
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);
回答by Aimal Miakhel
your view
<script>
<?php foreach($Prods as $Pr)
{
$arr[]=$Pr->Pname;
}
?>
$( function() {
var availableTags = <?php echo json_encode($arr); ?>;
$( "#tags" ).autocomplete({
source: availableTags
});
} );
</script>
and your conrtoller
public function create()
{
$Products = DB::table('Products')->get();
$Prods=DB::table('Products')->select('Pname')->get();
return view('test', ['Products'=>$Products],['Prods'=>$Prods]);
}