Search code examples
javascriptjqueryruby-on-railsjquery-select2jquery-select2-4

jquery select2 v4 - select default option on a select element


I'm using select2 v4 where I have the select2 hooked up to a <select> element using the ajax method. All works fine except I cannot default the selected value on load.

The examples page says:

If you need to provide default selections, you just need to include an option for each selection that contains the value and text that should be displayed.

In this code I want to default the value to "foo":

markup:

<label class="input">
  <select aria-hidden="true" tabindex="-1" 
    class="form-control user-school-select select2-hidden-accessible" 
    name="user[school_id]" id="user_school_id">
    <option selected="selected" value="1">foo</option>
  </select>
  <span style="width: 372px;" dir="ltr" 
    class="select2 select2-container select2-container--default">
    <span class="selection">
      <span aria-labelledby="select2-user_school_id-container" 
        tabindex="0" class="select2-selection select2-selection--single" 
        role="combobox" aria-autocomplete="list" aria-haspopup="true" 
        aria-expanded="false">
          <span title="foo" id="select2-user_school_id-container" 
            class="select2-selection__rendered"></span>
          <span class="select2-selection__arrow" role="presentation">
            <b role="presentation"></b></span></span></span>
 <span class="dropdown-wrapper" aria-hidden="true"></span></span></span>
</label>

this markup is generated by my rails view (users/edit.html.erb):

<%= label_tag nil, nil, :class => "input" do %>
  <% if @user.school_id %>
    <%= f.select :school_id, 
                 options_for_select({ @user.user_school => @user.school_id }, @user.school_id ), {}, 
                 { class: "form-control user-school-select" } %>
  <% else %>
    <%= f.select :school_id, {}, {},
                 {class: "form-control user-school-select"} %>
  <% end %>
<% end %>

javascript in users.js.erb:

$(document).ready(function() {
  $(".user-school-select").select2({
    ajax: {
        url: "/schools",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data, page) {
            return {
                results: data
            };
        },
        cache: true
    },
    escapeMarkup: function (markup) { return markup; }, 
    minimumInputLength: 2,
    templateResult: formatSchool,
    templateSelection: formatSchoolSelection
  }); 
});  

function formatSchool (school) {
  var markup = '<div class="clearfix">' +
        '<div class="col-sm-1">' +
        (school.logo_url ? '<img src="' + school.logo_url + '" style="max-width: 100%" />' : "") +
        '</div>' +
        '<div class="col-sm-10">' +
        '<div class="clearfix">' +
        '<div class="col-sm-9">' + school.name + '</div>' +
        '</div>';
  markup += '</div></div>';
  return markup;
} 

function formatSchoolSelection (school) {
  return school.name;
}

I can see in the select2 generated markup that the span has a title attribute populated with my value but the actual span text is empty:

<span class="selection">
  <span aria-labelledby="select2-user_school_id-container" tabindex="0" 
    class="select2-selection select2-selection--single" 
    role="combobox" aria-autocomplete="list" aria-haspopup="true" 
    aria-expanded="false">
    <span title="foo" id="select2-user_school_id-container" 
      class="select2-selection__rendered"></span>
    <span class="select2-selection__arrow" role="presentation">
      <b role="presentation"></b></span></span></span>

I have also tried the other deprecated method using initSelection to no avail:

$(".user-school-select").select2({
    ajax: {
        url: "/schools",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data, page) {
            return {
                results: data
            };
        },
        cache: true
    },
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
    minimumInputLength: 2,
    templateResult: formatSchool,
    templateSelection: formatSchoolSelection,
    initSelection : function (element, callback) {
        var data = {"id":"1","text":'foo'};
        callback(data);
    }
});
$(".user-school-select").select2("data", {"id": 1, "text": "foo"});

I've also tried adding this line after also to no avail:

$(".user-school-select").select2("val", "1");

I have looked at a bunch of answers to similar questions but they don't work for my scenario. I think because I am using select2 v4. Most other answers i've read say that you either need to use initSelection in combination with val - but those other examples are always using an <input> element rather than the <select>. I do note in the latest documentation that initSelection is depreciated and instead the current method of the DataAdapter should be used but I cant work out how I would apply this to my code above?


Solution

  • Not the right answer, but i had to apply a workaround - by removing the templateSelection method the problem is resolved. Not a good solution though because I want the benefit of the templateSelection method. I'll make do without it for now.

    $(document).ready(function() {
      $(".user-school-select").select2({
        ajax: {
            url: "/schools",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data, page) {
                return {
                    results: data
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, 
        minimumInputLength: 2,
        templateResult: formatSchool/*,
        templateSelection: formatSchoolSelection*/
      }); 
    });