How can I add data-attributes to the options using the jQuery Select2 framework?

0

This is what I have

$( "#codigo" ).select2({        
  ajax: {
    url: "getEmpleados.php",
    dataType: 'json',
    delay: 250,
    data: function (params) {
        return {
          q: params.term // search term
        };
    },
    processResults: function (data, page) {
      return {
        results: $.map(data, function (item) {
          return {
            id: item.id,
            text: item.text,
          }
        })
      };
    },
    cache: true
  },
  minimumInputLength: 1
});
    
asked by Edward Ceballos 06.03.2017 в 21:23
source

1 answer

-1

The data-attibutes are added in the HTML, example:

<select data-tags="true" data-placeholder="Select an option" data-allow-clear="true"></select>

Now, if you want to declare them in the JQuery statement, then it would look like this:

$("select").select2({
     tags: "true",
     placeholder: "Select an option",
     allowClear: true
});

If you want to fill in the Select2 using ajax and data-attributes it would be like this:

<select data-ajax--url="http://example.org/api/test" data-ajax--cache="true"></select>

Now, I am afraid to tell you that declaring the ajax from the data-attributes is very limited.

I leave you the documentation of the data-attributes here:

Select2 data-atributtes

Good luck!

    
answered by 06.03.2017 в 21:48