Select2 database

Asked

Viewed 230 times

0

I have a Select2 that is populated from the database of everything working, receives the data of the bank and etc. however if I want to edit it does not bring the saved option on the screen only on the front if debug it is there, already normal select it brings face understand

this is javascript

     $('.js-single').select2({
        allowClear: true,
        selectOnClose: true,
    })

this and html

<div class="es2 col-md-6">
              <label class="lb">País<label class="lb1" data-
            toggle="tooltip"
                title="ÁREA DE TEXTO OBRIGATÓRIO"> *</label></label> 
              <select data-toggle="tooltip"
                title="ÁREA DE TEXTO OBRIGATÓRIO" required="required"
                ng-model="pessoa.pessoasEnderecos.pais.idPais"
                ng-options="pai.idPais as pai.nome for pai in paises " 
              class="js-single"
                style="width: 100%; margin-top: -10px;" id="paisPais">
                <option ></option>
              </select>
            </div>

if anyone can help me

  • Hello. already tried to force change after value change? $('#paisPais').trigger('change');? maybe due to the angular the update is not triggering the change Trigger and the value does not appear on the screen.

  • Yes already tried, but when entering the modal nothing appears, only if I select the select then the correct option appears within a span. but in case it has to appear at the time I click edit open the modal and this is the option saved by the user, understand.

  • Researching a little I saw this reply that says we use another event $('#paisPais').trigger('change.select2'); maybe it works.

  • I took the test here on the select website and it really works when we use $('#paisPais').trigger('change.select2');', primeiro mudei o valor direto utilizando val("value")e nada mudou no select, depois doTrigger('change.Select2');` value has been updated in span.

1 answer

0

After changing the angular value, trigger the event change.select2 of Select2 itself

$('#paisPais').trigger('change.select2');

so the value will be updated in the whole structure of the element, including in the spans that you commented.

Browser other questions tagged

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