Search code examples
jqueryjquery-select2jquery-select2-4

JQuery, Select2 extra properties never returned in event


i'm working in select2 field with v4.0.3, everything work great instead when i want to get info from the data result when a selection are made, it supposed to be done with

$(this).select2('data')[0]

but i always get only the id and the text properties in the returned object, and i don't know if may a prob but the page use two select2 field but only this use ajax, about result i really got an extra property in the returned item of the result from the server, the result look like this [{"path":"/upload/images/filename","id":"11","text":"some text here"}, {"path":"/upload/images/another filename","id":"23","text":"still some text there"},{"path":"/upload/images/once filename again","id":"38","text":"some text to show"}]

i tried a much example found on google but nothing happen, now i'm out of idea, see my select2 code

$("#article_image").select2({
  placeholder: "Make your search...",
  allowClear: true,
  minimumInputLength: 2,
  ajax: {
    url: "url/to/search",
    dataType: "json",
    delay: 1250,
    data: function (e) {
            return {
                s: e.term,
                p: e.page
            };
        },
        processResults: function (data, p) {
          p.page = p.page || 1;

            return {
              results: data,
            }
      },
      cache: true
  },
  templateResult: format,
    escapeMarkup: function (m) { return m; }
}).on('select2:select', function (evt) {
  console.log($(this).select2('data')[0]);
});


Solution

  • It work now without change any line of code, it seem the cache option was the prob (not really sure cause i don't know js well) because i working in the same windows since a few days and this afternoon i lost the chrome session so i've just shutdown the pc, and now i just open my browser to see if i got answer on my question and trying some new way to handle the and just like a magic everything work great. Thx to Suresh Maurya for trying help me and hope this thread help someone