使用Laravel 4的Typeahead遥控器-我认为单个零件可以工作,但不能一起工作 [英] Typeahead remote with Laravel 4 - Individual pieces working I think, but not working together
问题描述
我对Laravel和PHP比较满意.我正在通过文档和SO/Google学习Laravel.我已经点击了我在这里可以找到的每个预想/laravel问题,但我似乎找不到合适的例子. 这个问题非常接近.我实际上看不到任何明显的差异.但是话又说回来,那个OP也有遥控器的麻烦.
I'm relatively green with Laravel and PHP. I'm learning Laravel by way of the documentation and SO/Google. I've clicked on every typeahead/laravel question I could find here, and I just can't seem to find an example that quite fits. This question comes very close. I actually don't see any glaring differences. But then again, that OP was having trouble with remote as well.
我想我接近了,因为我的各个作品似乎都是单独工作的.让我解释一下...
I think I'm close, as my various pieces seem to be working individually. Let me explain...
我有一个表格...
<div class="input-group form-group" id="ingredients">
{{ Form::label('ingredients', 'Ingredients') }}
{{ Form::text('ingredients[]', null, array(
'class' => 'form-control typeahead',
'data-provide' => 'typeahead',
'data-items' => '10',
'id' => 'ingredients')
) }}
</div>
...和脚本...
... with the script...
<script>
$('.typeahead').typeahead([
{
name: 'ingredients',
remote: '/recipe/create/%QUERY'
}
]);
</script>
remote:
指向此路线
Route::get('recipe/create/{query}', 'SearchController@searchIngredients');
哪个指向此控制器方法...
Which points to this controller method...
public function searchIngredients($query)
{
$data = array();
$results = Ingredient::select ('name')->where('name', 'LIKE', '%' . $query . '%')->get();
foreach ($results as $result) :
$data[] = $result->name;
endforeach;
var_dump($data);
return Response::json($data);
}
当我在表单中输入一些文本时,我确实看到了浏览器请求:
When I enter some text in the form, I do see the browser request:
如果我直接导航到远程URL,则确实会看到我的var_dump
:
If I navigate directly to the remote URL, I do see my var_dump
as I would expect:
例如/recipe/create/s
告诉我
array(4) {
[0] "Sugar"
[1] "Cheese"
[2] "Salt"
[3] "rasins"
}
我显然错过了一些东西,我怀疑这很简单,尽管在那里我也可能错了.当我开始在表单字段中输入内容时,没有下拉菜单.
I'm clearly missing something, and I have a suspicion that it's simple, though I could be wrong there too. When I begin typing in the form field, there is no dropdown.
我以为键入时永远不会调用我的控制器方法,否则我会在每个浏览器请求中看到var_dump
,对吗?
I'm thinking my controller method is never being called when typing, or I would be seeing the var_dump
with every browser request, right?
如果是这种情况,我在脚本的remote:
位处感到不舒服吗?
If that's the case, am I broken at the remote:
bit of the script?
编辑
推荐答案
尝试将var_dump
从控制器中取出-typeahead期望有效的JSON作为响应.
Try taking var_dump
out of the controller - typeahead is expecting valid JSON as a response.
这篇关于使用Laravel 4的Typeahead遥控器-我认为单个零件可以工作,但不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!