So, i have search wtform, one field of which is represented with select2. It is loaded dependently on another select field through ajax. Loading step works ok. But, when i submited my form, and received it back with result data, i cannot set selected value back to select2 field because i have only id, and, as i understood, callback method of initSelection requires both id and text. Or, maybe i am doing something wrong.
Here is select field
$("#source").select2({
placeholder: "Выберите источник",
initSelection: function(element, callback){
callback({id: element.val(), text:element.val()})
},
query: function(query){
query.callback(source_data)
}
})
And i want something like below. Data with such id already present in select2, the only question is how to get it selected.
callback({id: element.val())
element.val()
should still be in scope in the callback function, so you should just be able to pass it as a parameter, should you not? – Reinstate Monica Cellio