jquery select 2 options not getting formatted

痴心易碎 提交于 2019-12-12 03:35:55

问题


I have result of this format and could not get the options to load: can you please help me: what should result method look like?

          [{"id":2,"name":512843},{"id":2,"name":512844},
           {"id":2,"name":512847},{"id":2,"name":512874},
           {"id":2,"name":512875},{"id":2,"name":512880},
           {"id":2,"name":512884},{"id":2,"name":512886},
           {"id":2,"name":512887},{"id":2,"name":512888},
           {"id":2,"name":512892},{"id":2,"name":512896},
           {"id":2,"name":512907},{"id":2,"name":512910},
           {"id":2,"name":512911},{"id":2,"name":512913},
           {"id":2,"name":512914},{"id":2,"name":512917},
           {"id":2,"name":512925},{"id":2,"name":512930},
           {"id":2,"name":512931},{"id":2,"name":512935}]

it just says failed loading

        processResults: function (data) {
            return {
                results: $.map(data.items, function (item) {
                    return {
                        text: item.name,
                        id: item.id
                    }
                })
            };
        }

回答1:


I could figure out the answer,we should have output from json as

results = [{"id":2,"name":512847},{"id":2,"name":512874},
           {"id":2,"name":512875},{"id":2,"name":512880},
           {"id":2,"name":512884},{"id":2,"name":512886},
           {"id":2,"name":512887},{"id":2,"name":512888},
           {"id":2,"name":512892},{"id":2,"name":512896},
           {"id":2,"name":512907},{"id":2,"name":512910},
           {"id":2,"name":512911},{"id":2,"name":512913},
           {"id":2,"name":512914},{"id":2,"name":512917},
           {"id":2,"name":512925},{"id":2,"name":512930},
           {"id":2,"name":512931},{"id":2,"name":512935}]

when I have it so, everything was in place and select2 started working!



来源:https://stackoverflow.com/questions/43002082/jquery-select-2-options-not-getting-formatted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!