ajax - populate jquery select2 with original value -



ajax - populate jquery select2 with original value -

i using select2 plugin autocomplete text box. autocomplete makes ajax phone call employee view in database. working great except when user opens webform edit record. need select2 command load value saved record regardless of whether exists in list or not. when open edit form, select2 command blank. know can load value if in json info homecoming ajax call, if it's not? can see value in initselection don't know how set controls text/value it.

long story short, view making ajax phone call may or may not have original employee value in it. example, if employee selected view left branch/agency, view no longer show them. still need display original value in control. hope makes sense. here have code. can see original value in initselection don't know how in control:

var url = '@url.action("getemployees", "assetassignment")'; var originalvalue = ""; $('#assignedto').select2({ placeholder: "type mjb employee name or county...", minimuminputlength: 3, ajax: { url: url, datatype: 'json', type: "get", quietmillis: 50, data: function (term) { homecoming { term: term }; }, results: function (data) { homecoming { results: data.data }; } }, id: function (object) { // store text vs id homecoming object.text; }, createsearchchoice: function (term, data) { //allow manually entered text in drop down. if ( $(data).filter( function() { homecoming this.text.localecompare(term)===0; }).length===0) { homecoming { id: "", text: term, county: "" }; } }, initselection: function (element, callback) { var id = $(element).val(); if (id !== "") { //var id = element.val(); //var text = element.data('option'); //var info = { id: id, text: text }; //callback(data); //alert(json.stringify(data)) originalvalue = id; } }, formatresult: employeeformatresult, formatselection: employeeformatselection }); // other values selected item , set them in appropriate controls $('#assignedto').change(function () { var id = $('#assignedto').select2('data').id; var county = $('#assignedto').select2('data').county; $('#assignedtoemployeeid').val(id); $('#assignedtoemployeecounty').val(county); }); }); function employeeformatresult(data) { var markup = "<table class='select2-result'><tr>"; markup += "<td class='select2-result-id'>" + $.trim(data.id) + "</td>"; markup += "<td class='select2-result-text'>" + data.text + "</td>"; markup += "<td class='select2-result-county'>" + data.county + "</td>"; markup += "</tr></table>"; homecoming markup; } function employeeformatselection(data) { homecoming data.text; }

it figures. have been working on more day , half , 5 minutes after post question figure out. have set value with:

if (originalvalue.length > 0) $("#assignedto").select2("data", { id: originalvalue, text: originalvalue });

this works me because don't care if original value matches returned ajax call.

ajax asp.net-mvc jquery-plugins jquery-select2

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -