MyBB.select2();function dtFormatResult(dt){var markup ='<div class="clear">';if(dt.user_avatar) markup +='<div class="float_left useravatar">'+dt.user_avatar+'</div>';
markup +='<div class="float_left useravatar">'+dt.username+'</div>';
markup +='</div><br class="clear" />';return markup;}
$("#username").select2({
placeholder: "{$lang->search_user}",
minimumInputLength: 3,
maximumSelectionSize: 3,
multiple: false,
ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
url: "xmlhttp.php?action=get_users",
dataType: 'json',
data: function(term, page){return{
query: term,// search term};},
results: function(data, page){// parse the results into the format expected by Select2.// since we are using custom formatting functions we do not need to alter remote JSON datareturn{results: data};}},
initSelection: function(element, callback){var value = $(element).val();if(value !==""){
callback({
id: value,
text: value
});}},// Allow the user entered text to be selected as well
createSearchChoice:function(term, data){if( $(data).filter(function(){returnthis.text.localeCompare(term)===0;}).length===0){return{id:term, text:term, username:term};}},
formatResult: dtFormatResult
});
Now we should get the user avatar and the formatted username in the search list:
Then use our creativity to add more items to the format result. Example: