searchkick - 具有多个属性的自动完成 [英] searchkick - Autocomplete with multiple attributes

查看:10
本文介绍了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"
  });


});

是否需要在 typeahead js 文件中进行一些更改,因为要返回一组以上的数据?

Is some change required in the typeahead js file because of more than one set of data being returned?

推荐答案

你需要返回一个hash

您在 doctors 控制器中的 autocomplete 操作需要如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆