I'm using Select2 4.0.2 for CRUD pages. However, when I try to fill/initialize the selectbox
in the edit
page, it doesn't work for some reason. Why doesn't it work? Here's an example of what I have right now, it should be filled with e-mail address test@gmail.com. https://gyazo.com/92a89f5462c36a8c26f4dcd365fb1fb9
It should be something like this, but I don't get it to work: https://gyazo.com/194fa8b11a61736039a5acda763065ae
<script>
token = '{{csrf_token()}}';
$("#mainuser").select2({
ajax: {
dataType: "json",
type: "POST",
data: function (params) {
return {
term: params.term,
'_token': token,
isnew: 'false',
partner_id: {{$partner->id}}
};
},
url: '/partners/getusers',
cache: false,
processResults: function (data) {
return {
results: data
};
}
},
"language": {
"noResults": function () {
return "Geen gebruikers gevonden.<br> <a href='/klanten/nieuw' class='btn btn-danger'>Maak nieuwe klant</a>";
}
},
escapeMarkup: function (markup) {
return markup;
}
});
$("#mainuser").select2('data', {id: 1, text: 'test@gmail.com'});
// $("#mainuser").select2({...}).select2('data', {...}); doesn't work either...
</script>
What I have right now is the selectbox being populated by AJAX as expected, but not being filled (as how it should be in an edit
page). It should be already filled by the `.select2('data', {id:... text: ...}); part but it's not being filled with this value, as you can see in the video, it's still blank. I've read this article but it doesn't work.