问题
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