What is the correct way to reset select2 dropdown when using Ajax?

I am having a problem with select2 , where after entering the item and I want to use the same drop down again, t seems to delete the previously selected item

For example, you can say that my identifier is "#StreamName", I thought this would clear it:

 $("#StreamName").val(null).trigger("change");

but still it seems to display the previous selected item. From looking at the html it seems like I have brute force by doing this:

$("#select2-StreamName-container").html("");

but it looks like I'm not using the API correctly, so I wanted to see if there is another suggestion or if I am missing something.

+4
source share
1

-

, Select2 - , .

Javascript

$(document).ready(function() {
  $("#StreamName").select2();
  $("#StreamName").val(null).trigger("change");
});

HTML

<select id="StreamName">
  <option value="AL">Alabama</option>
  <option value="CA">California</option>
  <option value="WY">Wyoming</option>
</select>

-

JSFiddle

- , Select2 :

$("#StreamName").val(null).trigger("change");

Javascript

$(document).ready(function() {
  $("#StreamName").select2({
    ajax: {
      url: "https://api.github.com/search/repositories",
      dataType: 'json',
      delay: 250,
      data: function(params) {
        return {
          q: params.term, // search term
          page: params.page
        };
      },
      processResults: function(data, params) {
        // parse the results into the format expected by Select2
        // since we are using custom formatting functions we do not need to
        // alter the remote JSON data, except to indicate that infinite
        // scrolling can be used
        params.page = params.page || 1;

        return {
          results: data.items,
          pagination: {
            more: (params.page * 30) < data.total_count
          }
        };
      },
      cache: true
    },
    escapeMarkup: function(markup) {
      return markup;
    }, // let our custom formatter work
    minimumInputLength: 1,
    templateResult: formatRepo, // omitted for brevity, see the source of this page
    templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
  });


  $("#StreamName").val(null).trigger("change");

  // For testing...
  $('#set-null').click(function() {
    $("#StreamName").val(null).trigger("change");
  });



});


function formatRepo(repo) {
  if (repo.loading) return repo.text;

  var markup = "<div class='select2-result-repository clearfix'>" +
    "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
    "<div class='select2-result-repository__meta'>" +
    "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";

  if (repo.description) {
    markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  }

  markup += "<div class='select2-result-repository__statistics'>" +
    "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
    "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
    "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
    "</div>" +
    "</div></div>";

  return markup;
}

function formatRepoSelection(repo) {
  return repo.full_name || repo.text;
}

HTML

<select id="StreamName">
  <option value="3620194" selected="selected">select2/select2</option>
</select>
+9

Source: https://habr.com/ru/post/1618789/


All Articles