searchkick - 自动完成多个属性 [英] searchkick - Autocomplete with multiple attributes
本文介绍了searchkick - 自动完成多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果此处
Autocomplete works ok when searching with a single attribute as given here.
通过 - >(根据这个)
def autocomplete
Doctor.search(params[:query], autocomplete: true, limit: 10).map{|doctor| doctor.slice(:name, :city, :country) }
end
但是导致自动完成下拉列表/建议显示未定义。
However this results in the autocomplete dropdown/suggestions to show "undefined".
对于类型提前,我使用:
For type ahead i am using:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"></script>
在代码中引用:
$( function () {
$("#search").typeahead({
name: "doctor",
remote: "/doctors/autocomplete?query=%QUERY"
});
});
由于返回了多个数据集,在头文件js文件中需要更改某些变化? / p>
Is some change required in the typeahead js file because of more than one set of data being returned?
推荐答案
您需要返回哈希
您的自动完成
动作在医生
控制器需要看起来像这样:
Your autocomplete
action in doctors
controller need to look like this :
def autocomplete
render json: Doctor.search(params[:query], autocomplete: true, limit: 10).map do |doctor| { name: doctor.name, city: doctor.city, country: doctor.country }
end
end
在您的头像选项中添加 displayKey
Add displayKey
in your typeahead option:
$( function () {
$("#search").typeahead({
name: "doctor",
displayKey: 'name',
remote: "/doctors/autocomplete?query=%QUERY"
});
});
您还可以阅读这篇文章,看看它是否有帮助。
You can also read this article and see if it helps.
这篇关于searchkick - 自动完成多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文