First value of Select2 is not shown when you use i18next

Asked

Viewed 294 times

2

I am doing internationalization with the lib i18next, everything goes right, it translates puts the values within the select2, but the first value is not shown in any way, even if I open the select2 and clicking on the first item. Does anyone have any idea what might be going on?

HTML:

<html>
<head><title>Select2</title></head>
<body>
   <div>
      <label class="col-sm-12 text-white" style="text-align: left;" data-i18n="store.tipo"> </label>
      <div class="col-sm-12">
         <select id="selectTipo" class="form-control"></select>
         <br/>
         <br/>
      </div>
   </div>
   <script src="~/lib/jquery.js" type="text/javascript"></script>
   <script src="~/lib/select2.js" type="text/javascript"></script>
   <script src="~/js/arquivo.js" type="text/javascript"></script>
   <script type="text/javascript">
      $(document).ready(function(){
         GetTipo();
      })
   </script>
</body>
</html>

Javascript: (js file.)

function GetTipo(){
var $selectTipo = $('#selectTipo');

$selectTipo.append($('<option data-i18n="store.comboTipos.todos">', { value: "0", text: $(this).data('data-i18n')}));
$selectTipo.append($('<option data-i18n="store.comboTipos.painel">', { value: "1", text: $(this).data('data-i18n') }));
$selectTipo.append($('<option data-i18n="store.comboTipos.report">', { value: "2", text: $(this).data('data-i18n') }));
$selectTipo.append($('<option data-i18n="store.comboTipos.notificacao">', { value: "3", text: $(this).data('data-i18n') }));
$selectTipo.append($('<option data-i18n="store.comboTipos.auditoria">', { value: "4", text: $(this).data('data-i18n') }));
$selectTipo.append($('<option data-i18n="store.comboTipos.apresentacao">', { value: "5", text: $(this).data('data-i18n') }));
$selectTipo.val("0").trigger("change");
$selectTipo.select2();
}

IMAGE:

inserir a descrição da imagem aqui

JSFIDDLE: (link solved)

https://jsfiddle.net/danpadua/bq92ufq3/4/

  • You can make a fiddle out of it?

  • Done, this at the end of the question

  • In fiddle with select2 I don’t see anything.

  • Because eh, that’s the problem, here in my the lib Select2 was working more was not loading the data, now neither ta working

  • Now it’s working, look at it. Try to help me because it doesn’t load the items in the combo

1 answer

1


I managed to find a way:

As Voce is working with dynamic content occurs the library can not handle correctly.

after assembling the dynamic combo put these two guys down at the end, right after you added the dynamic fields.

$("#selectTipo").i18n();
$('#selectTipo').select2();
  • I’m gonna test......

  • It worked correctly, I updated Jsfiddle la

  • I have several Lects2 and sometimes some do not load the first element coming from i18n does not show, what else could it be?

  • if all are dynamic you need to instantiate them after loading

Browser other questions tagged

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