I'm trying to display images, along with the name, and ID from a database and displaying them in a select2 dropdown. I see in the console that the objects are coming through, but they are not displaying them?
Thank you for your help.
<select style="width: 100%" id="organization" class="form-control" name="organization-id">
<option value="" disabled selected>Find an organization</option>
</select>
function formatData (data) {
if (!data.id) { return data.name; }
var $result= $(
'<span><img src='+ data.avatar +'/> ' + data.name + '</span>'
);
return $result;
};
$( "#organization" ).select2({
ajax: {
url: "{{route('organization-search')}}",
type: "get",
dataType: 'json',
delay: 250,
data: function (params) {
return {
search: params.term // search term
};
},
processResults: function (data) {
return {
results: data
};
},
templateResult: formatData,
templateSelection: formatData,
cache: true
}
});