gpt4 book ai didi

javascript - 使用 pg_search 提前输入 Bloodhound Rails

转载 作者:行者123 更新时间:2023-12-03 08:13:53 26 4
gpt4 key购买 nike

我正在使用这个 stackoverflow answer尝试为我的 Rails 应用程序实现 Twitter Typeahead,我还尝试实现 Github自述文件,但当我在文本框中输入内容时没有文本建议。

我的 Controller

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

assets/javascripts 中的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 %>

最佳答案

在进行了一段时间的故障排除后,这是允许输入提示建议对我起作用的设置。

CoffeeScript

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()
})

Controller

    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 %>

关于javascript - 使用 pg_search 提前输入 Bloodhound Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com