【发布时间】:2017-09-13 19:37:45
【问题描述】:
我正在尝试设置 Typeahead.js,以用作我的 Laravel 应用程序的自动建议功能。不幸的是,它每次都没有返回任何结果。
我事先返回数据以利用本地存储,因此我的实例中没有查询数据库。
路线:
Route::get('/', function () {
return view('welcome', ['treatments' => Treatment::orderBy('treatment')
->pluck('treatment', 'id')]);
});
欢迎观看:
const treatments = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: '{{$treatments}}'
});
$('#bloodhound').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'treatments',
source: treatments,
templates: {
empty: [
'<div class="list-group search-results-dropdown"><div class="list-group-item">Nothing found.</div></div>'
],
header: [
'<div class="list-group search-results-dropdown">'
]
}
}).on('typeahead:selected', function (evt, item) {
$('#bloodhound').text(item.id);
});
输入栏:
<input type="search" name="treatment" id="bloodhound" class="form-control"
placeholder="Find a treatment" autocomplete="off" required>
$treatments 数组的输出:
local: '{"2":"Treatment 1"}'
脚本的最后一部分,应该在输入字段中输入选择的值 (ID),但不幸的是,这也不起作用。
非常感谢。
【问题讨论】:
标签: php laravel laravel-5 typeahead.js bootstrap-typeahead