javascript - Select 2 displaying undefined using initSelection ajax call -
i'm trying preload entry select2 using remote ajax call , initselection function. question seems similar this question, both answers don't work me.
the call works , returns correct result in json format, issue appears callback not sending data object in right format.
j("#selectelement").select2({ placeholder: "placeholder text", multiple: false, minimuminputlength: 1, ajax: { url: "/getfiles", datatype: 'json', data: function (term, page) { return { q: term //search term }; }, results: function (data, page) { return data; } }, initselection: function(element, callback) { return j.getjson("/getfiles?id=" + (element.val()), null, function(data) { if (j.isfunction(callback)) { //alert(json.stringify(data, null, 4)); return callback(data); } }); } }); when alert data object returns data:
{ "results": [ { "id": "1", "text": "name of file" } ], "more": "false" }
the end result text "undefined" being loaded select2 input.
i appreciate assistance.
tim
the solution format callback data this
return callback(data.results[0]); credit goes question/answer: select2 , initselection callback
Comments
Post a Comment