AJAX设置为Select2的默认选择值

时间:2019-04-05 15:59:15

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

我必须为下拉菜单设置默认值(传递GET变量)。 如何将其传递给HTML? 我尝试使用HTML中的经典“选定”标签,但无法正常工作。

HTML

<%=select_tag("asin_a", nil, required: true, include_blank: true, class:"form-control select2", "data-placeholder"=>"Search a product", "data-group_id"=> @group.id, "data-segment"=> "me")%>

Javascript

$('.select2').select2({
      ajax: {
      url: "/products/search",
      dataType: 'json',
      delay: 250,
      data: function (params) {
        console.log();
        return {
          query: params.term, // search term
          segment: $(this).data('segment'),
          group_id: $(this).data('group_id')
        };
      },
      processResults: function (data, params) {
        return {
          results: data.items
        };
      },
      cache: true
    },
    placeholder: "Select a product",
    allowClear: true,
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
    minimumInputLength: 0,
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
  });

0 个答案:

没有答案