【问题标题】:typeahead.js and rails search, remote option not displaying resultstypeahead.js 和 rails 搜索,远程选项不显示结果
【发布时间】:2014-08-02 03:49:34
【问题描述】:

我正在尝试完成一个项目的自动完成搜索栏(用于标题和照片描述)。我在这个项目上的合作伙伴已经在 Photos 控制器中实现了以下搜索操作

def search_results
  tag =  params[:q].values.first
  puts(tag)
  @photos= @q.result(distinct: true).to_a
  @photos += Photo.tagged_with(tag).flatten
  @photos.uniq!
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @photos }
    end 
end

//And in my application.js
$(document).ready(function(){
 $('#typeaheadBar').typeahead([
   {
     name: 'mysearch',
     displayKey: 'title',
     remote: '/search?utf8=%E2%9C%93&q%5Btitle_or_caption_cont%5D=%QUERY'
   }
 ]);

});

我看不出有什么问题,但我的导师说我很接近,我知道 typeahead 正在返回一些东西,但我不知道如何引用它。有人可以指出我正确的方向或指出我做错了什么吗?谢谢。

【问题讨论】:

    标签: javascript ruby-on-rails autocomplete typeahead.js bootstrap-typeahead


    【解决方案1】:

    typeahead 在数据集定义中没有remote 选项。查看可用选项here。您应该使用 Bloodhound 建议引擎从远程获取数据。您可以找到示例here。在你的情况下:

    var engine = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: '../data/films/queries/%QUERY.json'
    });
    
    engine.initialize();
    
    // The first argument is an option object, the additional arguments are 
    // the definitions of your datasets.
    $('#typeaheadBar').typeahead(null, {
      name: 'mysearch',
      displayKey: 'title',
      source: engine.ttAdapter()
    });
    

    【讨论】:

    • 谢谢!我通过将控制器更改为仅显示标题但实施您的建议来解决它,以便我可以搜索两者。感谢您指出 typeahead 也没有远程选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多