Anthony Conklin
Anthony Conklin

Reputation: 701

How do you modify "No Results Found" language in Select2 v4.0

I attempted to use the "language.noMatches" option when initiating Select2 and its throwing an undefined function? How do I go about modifying that bit of text? I would like to include a html button that would add the input from the user if it wasn't found. I tried doing this as a function as well as plain text. I also removed all html to see if that was doing it.

$('#search-select').select2({

   ...

   "language": {
       "noMatches": function(){
           return "No Results Found <a href='#' class='btn btn-danger'>Use it anyway</a>";
       }
   }
});

This was previously "formatNoMatches" in Select2 v3.5

Upvotes: 70

Views: 98263

Answers (4)

Arun Joseph
Arun Joseph

Reputation: 3154

The best solution right now is to make use of formatNoMatches option.

It is clean and concise.

Usage: you can pass either a string, or a function which returns a String to formatNoMatches

Example:

$('#search-select').select2({
...
formatNoMatches: "Nothing found"
...
})

or

$('#search-select').select2({
...
formatNoMatches: function () { return "Nothing found"; }
...
})

Ref: https://select2.github.io/select2/

Upvotes: 2

vishnumanohar
vishnumanohar

Reputation: 713

The option for select 2.5 seems formatNoMatches:

$('#search-select').select2({
  formatNoMatches: function () {
  return "No Results Found <a href='#' class='btn btn-danger'>Use it anyway</a>";
  }
});

Upvotes: 15

monteirobrena
monteirobrena

Reputation: 2620

Probably, you have to add the script for the language you want to use. Something like this:

<script src="select2/js/i18n/pt-BR.js" type="text/javascript"></script>

And then you can set the default language:

$(".select2").select2({
  "language": "pt-BR"
});

Upvotes: 50

Mariano Dupont
Mariano Dupont

Reputation: 1613

The option noMatches doesn't appear anywhere in the source code.

The actual option is named noResults. The working version of your example is:

$('#search-select').select2({

   ...

   "language": {
       "noResults": function(){
           return "No Results Found <a href='#' class='btn btn-danger'>Use it anyway</a>";
       }
   },
    escapeMarkup: function (markup) {
        return markup;
    }
});

You also need to override escapeMarkup, so the button appears correctly, as per this issue.

Upvotes: 151

Related Questions