简体   繁体   English

在jquery select2中更改文本“搜索”

[英]Change text “searching” in jquery select2

I am using select2 with ajax option, and when ajax runs, a text "searching" appearing until ajax get success. 我使用带有ajax选项的select2,并且当ajax运行时,出现文本“搜索”,直到ajax获得成功为止。 I want to change this text( see in attached image ). 我想更改此文本( see in attached image )。 在此处输入图片说明

My code is:(reference from https://select2.org ) 我的代码是:(从https://select2.org引用)

   $("#employee_id").select2({
      ajax: {
        url: "get_store_data.php?type=employee_data",
        dataType: 'json',
        delay: 250,
        data: function (params) {
          return {
            phrase: params.term, // search term,
            store_name: jQuery("#store_name").val(),
            page: 1
          };
        },

        processResults: function (data, params) {
            // console.log(data);
          params.page = params.page || 1;

          return {
            results: data.map(function(item) {
                return {
                    id: item.name,
                    text: item.name
                  };
            }),
            pagination: {
              more: 0
            }
          };
        },
        placeholder: 'Search for a member id',
        cache: true
      },
      escapeMarkup: function (markup) { return markup; }, 
      minimumInputLength: 1
    });

Use This. 用这个。 It's Help For You. 对您有帮助。

  $("#employee_id").select2({
    ajax: {
      type: 'get',
      url: 'get_store_data.php?type=employee_data',
      delay: 300,
      dataType: 'json',
      data: function (params) {
        return {
          search: params.term
        };
      },
      processResults: function (data) {
        return {
          results: $.map(data, function (obj) {
            return {
              id: obj.id,
              text: obj.text,
            };
          })
        };
      }
    },
    minimumInputLength: 2,
    placeholder: "Please Select",
    escapeMarkup: function (m) {
      return m;
    }
  });

I did not find any options so edited select2.min file. 我没有找到任何选项,因此编辑了select2.min文件。 find searching:function(){return"Searching"}}}) and change text here. 找到searching:function(){return"Searching"}}})并在此处更改文本。 It's working for me. 它为我工作。

Changed to: 变成:

searching:function(){return"Searching..."}}})

This is a translation issue I guess. 我猜这是翻译问题。 Try to include translation file and edit the text inside it. 尝试包括翻译文件并在其中编辑文本。 Create a file for ex en.js, place the code: 为ex en.js创建一个文件,放置代码:

    (function () {
if (jQuery && jQuery.fn && jQuery.fn.select2 && jQuery.fn.select2.amd) var e = jQuery.fn.select2.amd;
return e.define("select2/i18n/en", [], function () {
    return {
        errorLoading: function () {
            return "The results could not be loaded."
        }, inputTooLong: function (e) {
            var t = e.input.length - e.maximum, n = "Please delete " + t + " character";
            return t != 1 && (n += "s"), n
        }, inputTooShort: function (e) {
            var t = e.minimum - e.input.length, n = "Please enter " + t + " or more characters";
            return n
        }, loadingMore: function () {
            return "Loading more results…"
        }, maximumSelected: function (e) {
            var t = "You can only select " + e.maximum + " item";
            return e.maximum != 1 && (t += "s"), t
        }, noResults: function () {
            return "No results found"
        }, searching: function () {
            return "Searching…"
        }, removeAllItems: function () {
            return "Remove all items"
        }
    }
}), {define: e.define, require: e.require}})();

And edit those strings with translation you actually need. 然后使用您实际需要的翻译来编辑这些字符串。

Do not forget to include it right after the select2.js 不要忘记在select2.js之后添加它

And you probably will need to set language: "en" parameter too 而且您可能还需要设置language:“ en”参数

If understand you correctly, you want to change the text that appears whilst you are waiting (for the Ajax to complete. That text disappears after it completes. By default the text is "Searching...". The code below will change it to "Something else...". 如果正确理解您的要求,则想更改在等待时显示的文本(以完成Ajax。该文本在完成后会消失。默认情况下,该文本为“正在搜索...”。下面的代码会将其更改为“还有什么……”。

$("#employee_id").select2({
    language: {
        searching: function() {
            return "Something else...";
        }
    },
    ajax: {
        url: "get_store_data.php?type=employee_data",
        // etc.
    }
}

See the Select2 v4 documentation concerning Translation objects . 请参阅有关Translation对象的Select2 v4文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM