How to load JSON data to use it with select2 plugin
try this :
$.getJSON("djubrivo.php", function (json) {
$("#inputs").select2({
data: json,
width: "180px"
});
});
example json :
{
results:{
{id:0,text:"enhancement"},
{id:1,text:"bug"},
{id:2,text:"duplicate"},
{id:3,text:"invalid"},
{id:4,text:"wontfix"}
}
}
It appears you are using Select2 4.0, both from the link you provide to the examples and from the error message you are receiving. Your code, however, is written for previous versions of Select2.
If you want to continue to use Select2 4.0:
(1) Change the results
ajax option to processResults
.
(2) Change the processResults
function so the results
property of the object it returns is an array of objects, where each object has an id
and a text
property. One way to do this is to use the $.map()
function to create a new array from the one that is returned by the ajax call.
processResults: function (data) {
return {
results: $.map(data, function(obj) {
return { id: obj.ime, text: obj.ime };
})
};
}
You can also get rid of the formatResult
option.
(3) Use a <select>
element, instead of an <input>
element.
<select id="stavka" name="stavka" class="form-control js-example-responsive"></select>
jsfiddle