How to load JSON data to use it with select2 plugin

做~自己de王妃 提交于 2019-11-28 09:56:33

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

try this :

$.getJSON("djubrivo.php", function (json) {
      $("#inputs").select2({
         data: json,
         width: "180px"
      });
 });

example json :

        {id:0,text:"enhancement"},
        {id:1,text:"bug"},
        {id:2,text:"duplicate"},
        {id:3,text:"invalid"},
        {id:4,text:"wontfix"}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!