javascript - Select2 does not know the corresponding text of selected value -
i use remote data source options. when load form data server, contains values of select elements. in such situation, select2 not know corresponding text show user. there built-in reusable mechanism common scenario?
how set selected value/text when data fetched using ajax?
$('select#test').select2({ ajax: { url: "user/combo", datatype: 'json', delay: 250, cache: true } }); } }
indeed, trying create angular directive follow:
app.directive('mehrusercombo', ['$http', function ($http) { return { link: function (scope, element, attr) { element.select2({ ajax: { url: "user/combo", datatype: 'json', delay: 250, cache: true } }); } }
these ajax options :
ajax: { // number of milliseconds wait user stop typing before // issuing ajax request. delay: 250, // can craft custom url based on parameters passed // request. useful if using framework has // javascript-based functions generating urls make requests to. // // @param params object containing parameters used generate // request. // @returns url request should made to. url: function(params) { return urlgenerator.random(); }, // can pass custom data request based on parameters used // make request. `get` requests, default method, these // query parameters appended url. `post` requests, // form data passed request. other requests, // data returned here should customized based on jquery , // server expecting. // // @param params object containing parameters used generate // request. // @returns data directly passed request. data: function(params) { var queryparameters = { q: params.term } return queryparameters; }, // can modify results returned server, allowing // make last-minute changes data, or find correct part of // response pass select2. keep in mind results should passed // array of objects. // // @param data data returned directly jquery. // @returns object containing results data required // metadata used plugins. object should contain array of // data objects `results` key. processresults: function(data) { return { results: data }; }, // can use custom ajax transport function if not want use // default 1 provided jquery. // // @param params object containing parameters used generate // request. // @param success callback function takes `data`, results // request. // @param failure callback function indicates request // not completed. // @returns object has `abort` function can called abort // request if needed. transport: function(params, success, failure) { var $request = $.ajax(params); $request.then(success); $request.fail(failure); return $request; } }
on processresult function use :
processresults: function(data) { $('select#test').select2("val", value processed data); //set value return { results: data }; }
Comments
Post a Comment