javascript - How to reset the data after init , Select2 -
i use select2-4.0.0 have been struggling problem whole day update data.
i search every posts can update select2 data without rebuilding control, none have them work.
what need simple (a setter data):
i have diaglog, had select. every time open diaglog, ajax data keep in local array like:
when dialog open :
var select2list=synctoloadthedata();//data select2 $('#search-user-select').select2({ //here when secondly executed, select2's data on ui not refreshed data:select2list, matcher: function(params, data){ var key = params.term; if ($.trim(key) === '') { return data; } if( (matchkeyandpinyin(key,data.text))){ return data; } return null; } }
but problem though list changing, select options not change @ all.please note in test case, every time open dialog, data server changed:
what had tried: 1.when init:
data: function() { return {results: select2list}; }// not work show data @
2.when secondly open dialog:
$( "#search-user-select").select2('data',newdata,true);//not work have new data
3.when secondly open:
$("#search-user-select").select2("updateresults");//error, not have method
and other method directly change array's data(only 1 copy of data), none of them work.
i had same problem before, problem need update select2 after every ajax request new data.
and how fixed code.
eventid = $(this).attr('id'); $.ajax({ url: 'ajaxgetalleventperons', type: 'post', datatype: 'json', data: {id: eventid}, }) .done(function(data) { $("#select2_job").select2({ minimuminputlength: 2, multiple:true, initselection : function (element, callback) { //var data = data; callback(data); }, ajax: { url: "/ajaxgetallpersonforevent", datatype: 'json', quietmillis: 100, data: function (term) { return { term: term }; }, results: function (data) { var myresults = []; $.each(data, function (index, item) { myresults.push({ 'id': item.id, 'text': item.fullname }); }); return { results: myresults }; } } });
i hope example solve problem
Comments
Post a Comment