How to change the formatNoMatches in select2 4.0.0?

1

I'm using select2 4.0.0 and I'm having trouble changing the way the message that says I have not found results is shown. In the old version I used formatNoMatches to change the presentation, how is it done now? obs: I can not change this directly in the en-br.js file because every select behaves differently.

$('#id_categoria').select2(
    {
        placeholder:'Selecione',
        minimumInputLength: 2, 
        formatNoMatches: function(term) {
            return "Nenhum resultado. Adicionar " + term + "?";
        }
    }
);
    
asked by anonymous 24.03.2015 / 17:30

1 answer

1

It seems the default way to do this in version 4 is through the language file.

In the meantime I fucked a little and ended up developing a work-around that works:

$(function() {
  var id_categoria = $('#id_categoria')
    .select2({
      placeholder: 'Selecione',
      minimumInputLength: 2,
      language: $.extend({},
        $.fn.select2.defaults.defaults.language, {
          noResults: function() {
            var term = id_categoria
              .data('select2')
              .$dropdown.find("input[type='search']")
              .val();

            return $("<span>Nenhum resultado. <span class='add'>Adicionar <b>" + term + "</b></span>?</span>");
          }
        })
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><linkhref="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0-rc.2/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0-rc.2/js/select2.min.js"></script>

<select class="js-example-responsive" id="id_categoria" style="width: 50%">
</select>

I know it's not very pretty, but just so I was able to set the text dynamically for the select.

    
24.03.2015 / 18:49