Select2 display optgroup value and concatenated option

Asked

Viewed 344 times

1

I’m creating a selection field where the user can choose between multiple states, but I need to show when selected both the optgroup value and the option value, have as?

Remembering that I am using the library Select2

follows the base html

<select name="estados[]" id="estados" multiple  style="width:300px">
    <optgroup label="Suldeste">
        <option value="SP">São Paulo</option>
        <option value="RJ">Rio de Janeiro</option>
        <option value="MG">Minas Gerais</option>
        <option value="ES">Espírito Santo</option>
    </optgroup>
    <optgroup label="Sul">
        <option value="PR">Paraná</option>
        <option value="SC">Santa Catarina</option>
        <option value="RS">Rio Grande do Sul</option>
    </optgroup>
    <optgroup label="Nordeste">
        <option value="AL">Alagoas</option>
        <option value="BA">Bahia</option>
        <option value="CE">Ceará</option>
        <option value="MA">Maranhão</option>
        <option value="PB">Paraíba</option>
        <option value="PE">Pernambuco</option>
        <option value="PI">Piauí</option>
        <option value="RN">Rio Grande do Norte</option>
        <option value="SE">Sergipe</option>
    </optgroup>
    <optgroup label="Centro-Oeste">
        <option value="DF">Distrito Federal</option>
        <option value="GO">Goiás</option>
        <option value="MT">Mato Grosso</option>
        <option value="MS">Mato Grosso do Sul</option>
    </optgroup>
    <optgroup label="Norte">
        <option value="AM">Amazonas</option>
        <option value="AC">Acre</option>
        <option value="RR">Roraima</option>
        <option value="RO">Rondônia</option>
        <option value="AP">Amapá</option>
        <option value="PA">Pará</option>
        <option value="TO">Tocantins</option>
    </optgroup>
</select>

and the js call for Select2 to work

$("#estados").select2();

follows an example in jsfiddle

realize that when choosing a state it shows the name of the state, but would need to show both the region and the state, for example, Suldeste - São Paulo.

1 answer

1


Here on this reply So-English deals with your problem, specifically in this one jsfiddle which I thought was the best, but only works with simple selection.

With the help of the author of the question jsFiddle and with that question in English-OS we adapt the code to work for Multiple select;

Add:

function format(item) {

      var el = item.element;        
    var og = $(el).closest('optgroup').attr('label');    
    return og+'-'+item.text;
}

$("#estados").select2({
    formatSelection: format,
    escapeMarkup: function(m) { return m; }
});
  • Oops, I took a look and I’m sure you can help me, but there’s something strange that happens when I select a first option, it appears as Undefined the value of the optgroup label, do you have any idea? give a look at this updated http://jsfiddle.net/pb8b7f2b/1/ http:/jsfiddle.net

  • @Marcelodiniz has to be multiple selection?

  • In this case yes, it may be that the end user chooses an option or more.

  • And if you notice, if you choose a second option or even more other options it always returns the first label of optgroup

  • @Marcelodiniz understood, this code does not work correctly with multiple selection.

  • give a look at this one http://jsfiddle.net/pb8b7f2b/3/ dai update your answer so I mark it as valid, after all it was with your help

  • @Marcelodiniz It was top. If you want to edit you can be excited. I was trying with a friend at lunch, but I was fucking solve. Good that I could help.

  • 1

    @Marcelodiniz had even asked in the So-English. Look there. http://stackoverflow.com/questions/35361197/optgroup-value-multiple-select-with-selected2/35369650#35369650

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.