Hi
01 I want to set data attributes in json data. I am fetching data on page load and passing to select2
here is the code and how to select the default value. I am using 4.0.5 Version. Please help me I am new to select2.
02 If I used Ajax option in select2 it works when use click on select control. How can I preload it with data. As date is same not going to change and hardly few so Its better for me to fetched it once not every time use click on control.
var mydata = [
{ “id” = : 1, “text” : “AAA”, “data-mobile” : “1234”},
{ “id” = : 2, “text” : “BBB”, “data-mobile” : “2345”},
{ “id” = : 3, “text” : “CCC”, “data-mobile” : “3456”},
{ “id” = : 4, “text” : “DDD”, “data-mobile” : “4567”},
{ “id” = : 5, “text” : “EEE”, “data-mobile” : “5678”}
];
$(’#example-data’).select2({
placeholder: ‘Select a month’,
data : mydata
});
// ***************** Expecting output ************
<option value=“1” “data-mobile” = “1234” >AAA
<option value=“2” “data-mobile” = “2345”>BBB
<option value=“3” “data-mobile” = “3456”>CCC
<option value=“4” “data-mobile” = “4567”>DDD
<option value=“5” “data-mobile” = “5678” selected>EEE
Thanks in advance. It will reduced few io trip