Sampat Badhe
Sampat Badhe

Reputation: 9075

Select2 - Results not listing in dropdown using AJAX

I am using Select2 4.0.1, I have used ajax to populate the result based on users input, but whenever I search for anything select2 not listing any result, I am getting proper result in ajax response.

also it inputbox loss the focus after ajax response.

This is how I have initialized select2 -

  $('#select_tag_element').select2({
      closeOnSelect: false,
      multiple: true,
      placeholder: 'Assign a new tag',
      tags: true,
      tokenSeparators: [","],
      ajax: {
        url: "search_url",
        dataType: 'json',
        type: 'GET',
        delay: 250,
        data: function (params) {
          return {
            search: params.term,
            page: params.page
          };
        },
        processResults: function (data) {
          console.log(data)
          return { results: $.map( data, function(item) {
              return { id: item.id, text: item.name }
            })
          };
        },
        cache: true
      }
    });

I am getting the expected JSON Javascript Console.

[Object, Object, Object]
0: Object
id: 239
name: "Tag1"
__proto__: Object
1: Object
id: 255
name: "Tag2"
__proto__: Object
2: Object
id: 256
name: "Tag3"
__proto__: Object
length: 3
__proto__: Array[0]

Any help is much appreciated.Thnx:)

Update -

I debug and found it load the result in dropdown, but it removes result dropdown when globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );this line is executed in jquery.js

if ( fireGlobals ) {
  globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
  // Handle the global AJAX counter
  if ( !( --jQuery.active ) ) {
    jQuery.event.trigger( "ajaxStop" );
  }
}

How can I prevent result dropdown to close on above trigger?

===============================================================

Update

I am able to figure out this issue. this is due I was initializing the select2 on ajax success. so the result dropdown was hiding.

Upvotes: 2

Views: 1394

Answers (1)

Muhammad Waqas
Muhammad Waqas

Reputation: 1150

i just modified your code this will help you

$('#select_tag_element').select2({
  closeOnSelect: false,
  multiple: true,
  placeholder: 'Assign a new tag',
  tags: true,
  tokenSeparators: [","],
  ajax: {
    url: "search_url",
    dataType: 'json',
    type: 'GET',
    delay: 250,
    data: function (params) {
      return {
        search: params.term,
        page: params.page
      };
    },
    processResults: function (data) {
      var newData = [];
                    data.forEach(function (i,item) {
                        newData.push({
                            id: i.id  //id part present in data
                            , text: i.name  //string to be displayed
                        });
                    });

            return {
            results: newData
          };
    },
    cache: true
  },
   formatResult: function(data) {
        return data.name;
    },
    formatSelection: function(data) {
        return data.id;
    },
    escapeMarkup: function(m) {
        return m;
    }
});

Upvotes: 2

Related Questions