【问题标题】:Select2 not loading dataSelect2 不加载数据
【发布时间】:2018-10-31 04:06:12
【问题描述】:

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

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

【问题讨论】:

  • @debugger 我在另一个操作中定义了 client_params,这会影响搜索操作吗?
  • @dbugger 我已经添加了代码
  • @dbugger 如果我手动调用搜索操作,无论有无参数,它都可以正常工作
  • @dbugger 来自日志,select2 没有运行 ajax 调用,也没有运行 initSelection
  • @marimaf 您能否分享您的 select2 所在的生成页面的完整代码?

标签: ruby-on-rails ruby-on-rails-5 jquery-select2


【解决方案1】:

问题是我使用的是 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 }

还有咖啡脚本:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多