JQuery Select2 Data Attributes

I implemented a search from a list of options through Ajax and it works fine. However, to fill in other cascading selections, I would save some html5 "data" attributes according to the filled parameters. The "results" method expects only an array with identifiers and texts. Any idea?

    results: function(data, page) {
      return { results: $.map(data, function(city, i) {
        return {id: city.id, text: city.name};
        // I tried adding data- attributes to return but didn't work
      })}
    }
  }
+4
source share
1 answer

You can use formatResult . http://jsfiddle.net/LUsMb/4098/

$("#e4").select2({
    formatResult: function(state) {
        return '<div data-my="' + ... + '">' + state.text + '</div>';
    },
    ...
});
+3
source

All Articles