Akond
Akond

Reputation: 9

select2 and data on event selecting

I have the following code with select2 and I can not get back the data that is in the json in the event of selecting

<script>
var $input = $('#player_list');
$input.select2({
  placeholder: "Player...",
  minimumInputLength: 2,
  ajax: {
      url: '/{{ $region }}/search',
      dataType: 'json',
      data: function (params) {
          return {
              q: $.trim(params.term)
          };
      },
      processResults: function (data) {
          return {
              results: data
          };
      },
      cache: true
  }
});
$input.on('select2:selecting', function(e){
    window.location = e.params.data.url;
});
</script>

The autocomplete function works fine, but i cant manage for the link, seems e.params.data.url is undefined, Ajax returning example:

[{"id":"Thevile","text":"Thevile","url":"\/eu\/player\/dun-modr\/Thevile"}]

Thx.

Upvotes: 0

Views: 699

Answers (2)

Evangelito
Evangelito

Reputation: 51

Explanation : the event : select2:selecting is triggered just before the event select2:select ... so the data in select2 input is not setting yet : that's why the data is still undefinided

The solution : to get the data you have to choose the event select2:select.

Upvotes: 2

Akond
Akond

Reputation: 9

Worked fine with event select, just changed select2:selecting for select2:select and worked

Problaby is a bug of library or something

Upvotes: -1

Related Questions