marcopal
marcopal

Reputation: 45

Limit Select2 to one selection per optgroup with multiple selection in version 4.0

I have a select box with multiple optgroup like:

  <select id="g1" class="select"  multiple="multiple">
    <option></option>
    <optgroup label="Group 1">
      <option value="1">Title 1 A</option>
      <option value="2">Title 1 B</option>
      <option value="3">Title 1 C</option>
    </optgroup>
    <optgroup label="Group 2">
      <option value="4">Title 2 A</option>
      <option value="5">Title 2 B</option>
      <option value="6">Title 2 C</option>
    </optgroup>
  </select>

I would need to limit my selectbox to one option per optgroup, replacing the previous selected option, example:

If I select "Title 1 A" from the first optgroup and "Title 2 A" from the second optgroup, then I select "Title 1 C" from the first optgroup the result is "Title 1 C" and "Title 2 A".

I'm using Select2 v4.0.3.

I found this solution: Limit select2 selections by group But is for Select2 v3.5.2 and it's not compatible with the new version.

Upvotes: 1

Views: 2567

Answers (1)

Bindrid
Bindrid

Reputation: 3735

Ok, I came up with a solution that you can probably adapt. Optgroups do not figure in but should work anyway.

HTML:

<select multiple style="width: 300px">
    <option groupid="a" value="A_AK">Alaska</option>
    <option groupId="b" value="B_HI">Hawaii</option>
    <option groupid="c" value="C_CA">California</option>
    <option groupid="a" value="D_NV">Nevada</option>
    <option groupid="b" value="A_OR">Oregon</option>
    <option groupid="c" value="B_WA">Washington</option>
    <option groupid="a" value="C_AZ">Arizona</option>
    <option groupid="b" value="D_CO">Colorado</option>
    <option groupid="c"value="A_ID">Idaho</option>
    <option groupid="a" value="B_MT">Montana</option>
    <option groupid="b" value="C_NE">Nebraska</option>
    <option groupid="c" value="D_NM">New Mexico</option>
    <option groupid="a" value="A_ND">North Dakota</option>
    <option groupid="b"value="B_UT">Utah</option>
    <option groupid="c" value="C_WY">Wyoming</option>
</select>

You will notice that I added the group id attribute. This is used to establish the relationship between the options.

JavaScript:

 $(function() {
    $('select').select2({
      allowClear: true,
      placeholder: "Pick a State"
    });

    //Select2 Event handler for selecting an item
    $('select').on("select2:selecting", function(evt, f, g) {
      disableSel2Group(evt, this, true);
    });

    // Select2 Event handler for unselecting an item
    $('select').on("select2:unselecting", function(evt) {
      disableSel2Group(evt, this, false);
    });
  });


  // At some point during the select2 instantation it created the 
  // data object it needs with the source select option.
  // This function, called by the events above to set the current status for the
  // group for which the selected option belongs.
  function disableSel2Group(evt, target, disabled) {
    // Found a note in the Select2 formums on how to get the item to be selected

    var selId = evt.params.args.data.id;
    var group = $("option[value='" + selId + "']").attr("groupid");

    var aaList = $("option", target);
    $.each(aaList, function(idx, item) {
      var data = $(item).data("data");

      var itemGroupId = $("option[value='" + data.id + "']").attr("groupid");
      if (itemGroupId == group && data.id != selId) {
        data.disabled = disabled;
      }
    })
  }

Upvotes: 0

Related Questions