繁体   English   中英

用pg_search提前输入猎狗轨道

[英]Typeahead Bloodhound Rails with pg_search

我正在使用这个stackoverflow 答案来尝试为我的rails应用程序实现Twitter Typeahead,并且我还尝试了实现Github自述文件,但是当我在文本框中键入内容时没有文本建议。

我的控制器

class PagesController < ApplicationController
  def typeahead

    @vn = Vn.search_by_name(params[:search])
    render json: @vn.results
  end
end

我的路线

get 'typeahead/:query' => 'pages#typeahead'

Application.js

//= require twitter/typeahead
//= require twitter/typeahead/bloodhound

资产/ javascript中的pages_controller.js

var bloodhound = new Bloodhound({
  datumTokenizer: function (d) {
    return Bloodhound.tokenizers.whitespace(d.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,

  // sends ajax request to /typeahead/%QUERY
  // where %QUERY is user input
  remote: '/typeahead/%QUERY', 
  limit: 50
});
bloodhound.initialize();

// initialize typeahead widget and hook it up to bloodhound engine
// #typeahead is just a text input
$('.typeahead').typeahead(null, {
  displayKey: 'name',
  source: bloodhound.ttAdapter()
});

// this is the event that is fired when a user clicks on a suggestion
$('.typeahead').bind('typeahead:selected', function(event, datum, name) {
  doSomething(datum.id);
});

我的观点

   <%= form_tag(search_path, :method => "get", class: "navbar-form", id: "search-form") do %>
      <%= text_field_tag :search, params[:search], class: "form-control padding-search typeahead", placeholder: "Search" %>   
      <%= button_tag(type: "submit", class: "btn btn-primary padding-search") do %>          
   <% end %>

经过一段时间的故障排除后,这些设置可使Typeahead建议对我有用。

咖啡脚本

jQuery -> 
  users = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  #local http://localhost:3000/json/vns.json
  remote: {url:'/typeahead/%QUERY'
   , wildcard: '%QUERY'
   }
  })

  users.initialize();
  $('.typeahead').typeahead(null, {
      name: "mysearch"
      source: users.ttAdapter()
  })

控制者

    def typeahead    
    @vn = Vn.search_by_name(params[:search])
    @name = @vn.select('name').map(&:name)

    render json: @name
  end
  //Under Routes
  get 'typeahead/:search' => 'pages#typeahead'
  //View
 <%= form_tag(search_path, :method => "get", class: "navbar-form", id: "search-form" ) do %>
              <%= text_field_tag :search, params[:search], class: "form-control typeahead", placeholder: "Search " %>
              <%= button_tag(type: "submit", class: "btn btn-primary padding-search") do %>
                  <i class="fa fa-search 6x"></i>
              <% end %>
 <% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM