Select2 Preloading Multiple Selection from AJAX loop -
i'm integrating google map api uses geonames , select2 allow user enter cities he/she has visited.
currently, trying find way search area show selections user made in previous session upon reloading page (e.g., if user entered paris, france in previous session, paris, france should preloaded in search area upon reloading). selections stored in database, @ moment i'm able put 1 of selected cities in search area repassing through geonames (i need pass through geonames pass lat & long). i'd repass many locations user entered in previous session.
the code using below - help:
function locationformatresult(location) { return location.name + ", " + location.adminname1 + ", " + location.countryname; }//results format function locationformatselection(location) { return location.name + ", " + location.adminname1 + ", " + location.countryname; }//selection format $(function () { $('#citiestext').select2({ id: function(e) { return e.name + ',' + e.adminname1 + ',' + e.countryname + ',' + e.lat + ',' + e.lng}, placeholder: 'location', multiple: true, allowclear: true, width: '350px', height: '50px', overflow: 'auto', minimuminputlength: 2, ajax: { //this ajax call when user selects city url: 'http://ws.geonames.org/searchjson', datatype: 'jsonp', data: function (term) { return { featureclass: "p", style: "medium", isnamerequired: "true", q: term }; }, results: function (data) { return { results: data.geonames }; } }, initselection : function(element, callback){ (var i=11;i<13;i++){ $.ajax("http://ws.geonames.org/searchjson",{//ajax preloading datatype: 'jsonp', data:{ maxrows:1, q: i}//for example, i'm using numbers 11 & 12 geonames queries test preloading functionality (both numbers have corresponding locations in geonames if run through query) }).done(function(data){callback(data.geonames);}); //problem: returning location "12" - need return locations 11 , 12 in select area }}, formatresult: locationformatresult, formatselection: locationformatselection, dropdowncssclass: "bigdrop", escapemarkup: function (m) { return m; } }); }); jsfiddle: http://jsfiddle.net/ydjee/ (trying more 1 entry in select box)
the point callback needs called array of objects multiple select2.
in case, need gather each json object ajax call, need use jquery deferreds.
something that:
initselection : function(element, callback){ var result = new array(); var f = function(i) { return $.ajax("http://ws.geonames.org/searchjson",{ datatype: 'jsonp', data:{ maxrows:1, q: }, success: function(data) { result.push(data);}}) }; var def = []; (var i=11;i<13;i++){ def.push(f(i)) }}; $.when.apply($, def).done(function() { callback(result); }); }
Comments
Post a Comment