Prevent reload of data with select2 plugin (v4)

最后都变了- 提交于 2019-11-30 19:58:27

问题


I'm using select2 plugin(v4) and loading his options with ajax.
If i open the select input on the first time the data will be loaded. When I open the select again, the same data is reloaded.

There is any way to prevent this "reloading" of data? I mean, if i just open the select2 and the options were previously loaded I don't want to load again.

Here is my current code:

$(select_input).select2({
  ajax: {
    url: url,
    dataType: 'json',
    quietMillis: 250,
    data: function (params) {
      return {
        term: params.term,
        page: params.page,
        page_limit: default_page_size
      };
    },
    processResults: function (data) {
      return {
        results: data.data,
        pagination: {
          more: (data.page * default_page_size) < data.count
        }
      };
    },
    cache: true
  },
  escapeMarkup: function (markup) {
    return markup;
  },
  minimumInputLength: 0,
  templateResult: formatTemplateResult,
  templateSelection: formatTemplateSelection
});

回答1:


Since you are using select2_v.4, you have to use data-adapters ( query and initSelection methods were depreciated in V.4.0 ) Following is an example of using custom-data-adapter through an approach called AMD-Pattern. I have had not a chance to test it but I believe this would give you a direction to proceed with.

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.min.js"></script>
<script>
    var cachedData;
    $.fn.select2.amd.define('select2/data/customAdapter', ['select2/data/array', 'select2/utils'],
       function (ArrayAdapter, Utils) {
           function CustomDataAdapter($element, options) {
               CustomDataAdapter.__super__.constructor.call(this, $element, options);
           }

           Utils.Extend(CustomDataAdapter, ArrayAdapter);

           CustomDataAdapter.prototype.query = function (params, callback) {
               var retData;
               if (cachedData != undefined) // check if data existing in cachedData variable
                   retData = cachedData;
               else {
                   $.ajax({
                       url: url,
                       dataType: 'json',
                       quietMillis: 250,
                       data: function (params) {
                           return {
                               term: params.term,
                               page: params.page,
                               page_limit: default_page_size
                           };
                       },
                       success: function (data) {
                           retData = {
                               results: data.data,
                               pagination: {
                                   more: (data.page * default_page_size) < data.count
                               }
                           };
                           cachedData = retData; // save the retData in a global variable
                       },
                       error: function (er) {
                           debugger;
                       },
                       cache: true
                   });
               }

               callback(retData);
           };

           return CustomDataAdapter;
       }
);


    $(document).ready(function () {
        var customAdapter = $.fn.select2.amd.require('select2/data/customAdapter');
        $("#select1").select2({
            tags: true,
            dataAdapter: customAdapter,
            escapeMarkup: function (markup) {
                return markup;
            },
            minimumInputLength: 0,
            templateResult: formatTemplateResult,
            templateSelection: formatTemplateSelection
        });
    });
</script>


来源:https://stackoverflow.com/questions/33949508/prevent-reload-of-data-with-select2-plugin-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!