I'm trying to make an edit form. This form must load data that comes from a database and show the data as it comes from it. In my form, I have a select2 combo-box that loads the items via ajax. I need to change the selected index of this combo box using the id value that comes from the database. The code that I use to load the data is:
function populateState(){
var urlCities='@routes.StateCtrl.lstStatesByCountryToSelect()';
$("#idCountry").select2({
placeholder: "Select country...",
ajax: {
url: urlCities,
dataType: 'json',
type: 'GET',
data: function(term,page){
return{q:term,idCountry:$("#idCountry").val()};
},
results: function(data, page) {
return {
results: data.results
};
}
}
});
}
I was trying with this but it did not work:
$("#idCountry").select2().select2("val", id);
For example if I load three states in the combo box (Texas, New York, Florida) when the page loads, I want to show in the combo box Texas as the initial selected item.