gpt4 book ai didi

ruby-on-rails - Select2 不加载数据

转载 作者:行者123 更新时间:2023-12-04 07:32:10 26 4
gpt4 key购买 nike

我最近升级到 Rails 5.2 和 ruby​​ 2.5.1,我的选择框停止工作了。

我不得不在我的 admin.js 中更改 requiere 文件并添加完整版本以查看选择框

//= 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
})

我的 Controller 搜索端点:

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"
}
]
}

选择输入所在的 View 源代码摘录:

<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使 Controller 响应但没有改变。我还尝试将 dataType: 'json' 作为 suggested here 添加到 ajax 调用中在第一个 select2 的末尾添加 .select2('val',[])我尝试手动添加 { id: '1', text:'example' } initSelection我在控制台中没有收到任何错误

但是 ajax 调用似乎不起作用。在控制台中,没有对搜索端点的调用。

最佳答案

问题是我使用的是 select2 4.0,代码是为 3.5 设计的

我必须将我的输入更改为一个选择,并在其中添加一个 multiple="multiple"属性,而不是将其声明为 select2 选项的一部分。这是使其发挥作用的主要变化。之后进行了一些调整,我不得不更改 initSelection 格式化程序并将 ajax 结果从结果更改为 processResults。最终代码是这样的:

%select#publish_to{ :name => 'publish_to', :multiple => 'multiple', :type => 'hidden', "data-lookup-endpoint" => my_endpoint_path }

还有 CoffeeScript :

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'

Controller ,将参数从 [: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

关于ruby-on-rails - Select2 不加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076137/

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