Skip to content Skip to sidebar Skip to footer

How To Load Json Data To Use It With Select2 Plugin

I want to use select2 plugin for my project. I followed this example, but it doesn't work for me. JSON output : [ {'ime':'BioPlex TM'}, {'ime':'Aegis sym agrilla'}, {'i

Solution 1:

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

Solution 2:

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"}
      }
    }

Post a Comment for "How To Load Json Data To Use It With Select2 Plugin"