Is there a way I can set selected id/text in select2 in HTML just like is being displayed and selected?
I am using jquery select2 version 4.0.0
this is the code:
$("#users").select2({
placeholder: "select user...",
escapeMarkup: function (markup) { return markup; },
ajax: {
url: "users.json",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (response, page) {
return {
results: response.data
};
},
cache: true
},
templateResult: function(item){
if (item.loading) {
return item.text;
}
return item.username + " <small><i>(" + item.role + ")</i></small> "; // display data in html
},
templateSelection: function(item){
if (item.username){
return item.username + " <small><i>(" + item.role + ")</i></small> "; // select row in html
}
return item.text; // for placeholder
}
});
To set selected value I do:
// bring data for to set selected value, data contains fields data
var selected_user_id = data.id,
// this is the content that I need to use as it is HTML content
selected_user_text_html = data.username + " <small><i>(" + data.role + ")</i></small> ",
// this is the one that I am using and is not html because there is no way to put html inside a option select item
selected_user_text_regular = data.username + " (" + data.role + ") ";
// set selected value
$("#users").append("<option value='"+selected_user_id+"' selected='selected'>"+selected_user_text_regular+"</option>");
// trigger update
$("#users").trigger("change");
Is there a way I can set the selected text in HTML instead of plain text?
Yes, You can. Select2 has a two helpful options "templateSelection" and "templateResult" Both are functions and you may create any html wrapper you wish. Just pass options to select2() method with something like this..
var data = [{id: 100, text: "AAAAAA"}, {id: 200, text: "BBBB"}]
var formatState = function(result) {return $("<b>"+result.text+"</b>")}
var formatSelection = function(selection){return $("<b>"+selection.text+"</b>") }
$(".js-example-basic-single").select2({ data:data,templateResult: formatState, templateSelection: formatSelection
}).val(100).change();
And you should see the the option with id:100 is selected.
I hope I helped you;
if i understand you correctly,
According to the api https://select2.github.io/examples.html , you can set the selected by putting it in the html markup and then calling the javascript
var data = [
{ id: 0, text: 'enhancement' },
{ id: 1, text: 'bug' },
{ id: 2, text: 'duplicate' },
{ id: 3, text: 'invalid' },
{ id: 4, text: 'wontfix' }
]
$(".example").select2({
data: data
})
<select class="example">
<option value="3" selected="selected">invalid</option>
</select>
Update
Assuming the ajax is called on declaration
would you not be able to append the option when the ajax returns.
ajax: {
...
processResults: function (response, page) {
//something like
$.each( response.data, function( i, obj ) {
if(obj.selected)
{
var elem = $('#users')
elem.append($('<option>', {
value: obj.value,
text : obj.text,
selected : "selected"
}));
}
})
return {
results: response.data
};
},
cache: true
},
Also update
consider the following for replace your other code or giving you ideas
<style>
.optionrole{
font-size: small;
font-style: italic
}
</style>
var sUserId = data.id,
var sUserText = data.username
+ "<span class="optionrole"> ("
+ data.role
+ ")</span> ";
// set selected value
var elem = $('#users')
elem.append($('<option>', {
value: sUserId,
text : sUserText,
selected : "selected"
}));
// trigger update
elem.trigger("change");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With