我最近升级到Rails 5.2和ruby 2.5.1,我的选择框停止工作。
我必须在admin.js
中更改要求文件,并添加完整版本,甚至查看选择框
//= require select2-full
,但是现在获取数据甚至进行初始选择的ajax查询都没有显示任何结果。我正在使用Gon将数据传递到我的js,并检查了gon是否具有正确的值。我的js.coffee如下:
clientFormatResult = (client) ->
client.email
clientFormatSelection = (client) ->
client.email
$ ->
$select_input = $('#publish_to')
$select_input.val(' ')
$select_input.select2({
width: '420px'
height: 'auto'
placeholder: "Search"
minimumInputLength: 2
multiple:true
closeOnSelect: true
tokenSeparators:[',',' ']
ajax: {
url: $select_input.data('search-endpoint')
data: (term, page) ->
{
q: term # search term
page_limit: 10
}
results: (data, page) -> # parse the results into the format expected by Select2.
console.log("result")
# since we are using custom formatting functions we do not need to alter remote JSON data
results: data.manage_clients
}
initSelection: (element, callback) ->
callback(gon.published_to)
formatResult: clientFormatResult
formatSelection: clientFormatSelection
})
我的控制器搜索端点:
def search
clients = Client.where("email like '%#{params[:q]}%'").map { |c| { :id => c.id, :email => c.email } }
render :json => clients
end
private
def client_params
params.require(:client).permit(:email, :password, :password_confirmation, :first_name, :last_name, :organisation, :assigned_team)
end
搜索端点结果:
{
"manage_clients": [
{
"id": 46,
"email": "aaaa@example.net"
},
{
"id": 365,
"email": "bbbb@example.org"
}
]
}
提取选择输入为以下位置的视图源代码:
<div class="input string required publish_to">
<label class="string required control-label" for="report_clients">
Publish to
</label>
<input data-search-endpoint="/my-endpoint-search" id="publish_to" name="publish_to" type="hidden" value="" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true"><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 420px;"><span class="selection"><span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1"><ul class="select2-selection__rendered"><li class="select2-search select2-search--inline"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Search for a client" style="width: 418px;"></li></ul></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
<div>
<a class="add_button button" href="/some_url">Action</a>
</div>
</div>
我已经尝试过将控制器的响应设置为suggested here的解决方案
但没有变化。
我还尝试将suggested here添加到dataType: 'json'
到ajax调用中
在第一个select2的末尾添加.select2('val',[])
我尝试将{ id: '1', text:'example' }
手动添加到initSelection
我在控制台中没有任何错误
但是ajax调用似乎不起作用。在控制台中,没有对搜索端点的调用。
答案 0 :(得分:2)
问题是我使用的是select2 4.0,而代码是为3.5设计的
我不得不将输入更改为select,并在其中添加了一个multiple =“ multiple”属性,而不是将其声明为select2选项的一部分。那是使其工作的主要变化。之后,我不得不更改initSelection格式化程序并将ajax结果从结果更改为processResults。最终的代码是这样的:
%select#publish_to{ :name => 'publish_to', :multiple => 'multiple', :type => 'hidden', "data-lookup-endpoint" => my_endpoint_path }
还有咖啡脚本:
formatValues = (values) ->
data = []
i = 0
while i < values.length
data.push
id: values[i].id
text: values[i].email
i++
data
$ ->
$select_input = $('#publish_to')
$select_input.val('')
$select_input.select2({
placeholder: 'Search for a Client'
width: '420px'
height: 'auto'
data: formatValues(gon.published_to)
allowClear: true
minimumInputLength: 2
tokenSeparators:[',',' ']
ajax: {
url: $select_input.data('lookup-endpoint')
dataType: 'json'
data: (term, page) ->
{
q: term # search term
}
processResults: (data, page) -> # parse the results into the format expected by Select2.
# since we are using custom formatting functions we do not need to alter remote JSON data
results: formatValues(data.manage_clients)
}
})
# To set initial values:
$('#publish_to').val(getIds(gon.published_to)).trigger 'change'
控制器,将参数从[:q]
更改为[:q][:term]
:
def search
clients = Client.where("email like '%#{params[:q][:term]}%'").map { |c| { :id => c.id, :email => c.email } }
render :json => clients
end