Override Minimum length string of Select2

前端 未结 4 1604
梦毁少年i
梦毁少年i 2021-01-01 14:13

Select2 Jquery Plugin

I was having hard time how to override the default message for minimum length input in jquery Select2.

by default the plugin gives th

4条回答
  •  囚心锁ツ
    2021-01-01 15:09

    The accepted answer does not work for Select2 v4. Expanding on the comment by @IsaacKleinman, the way to override the default messages for an individual Select2 instance is through the language property:

    var opts = {
      language: {
        inputTooShort: function(args) {
          // args.minimum is the minimum required length
          // args.input is the user-typed text
          return "Type more stuff";
        },
        inputTooLong: function(args) {
          // args.maximum is the maximum allowed length
          // args.input is the user-typed text
          return "You typed too much";
        },
        errorLoading: function() {
          return "Error loading results";
        },
        loadingMore: function() {
          return "Loading more results";
        },
        noResults: function() {
          return "No results found";
        },
        searching: function() {
          return "Searching...";
        },
        maximumSelected: function(args) {
          // args.maximum is the maximum number of items the user may select
          return "Error loading results";
        }
      }
    };
    $('#mySelect').select2(opts);
    

    To override the functions globally, call the set function on the defaults (according to the docs):

    $.fn.select2.defaults.set("key", "value")
    

    However, in our code we do it like this:

    $.fn.select2.defaults.defaults['language'].searching = function(){
      return 'Custom searching message'
    };
    

    I don't know why we don't follow the docs, but it works.

提交回复
热议问题