You need to arrange for the rendered “empty” item to have some invisible content. You can do that with a custom templateResult function like the following:
const renderItem = item => item.text || '\u200B';
// ... Initialize the select2
$('#my_select').select2({
// configuration options
renderTemplate: renderItem,
// more configuration options
});
This outputs the item’s text if it’s not the empty string; otherwise it outputs \u200B
, the Unicode zero-width space character. You could alternatively output the regular non-breaking space, e.g.,
, or any other “invisible” character (but not the regular space: HTML rendering will treat that like no content at all).
Of course your rendering function could return a more elaborate DOM fragment (if you need special styling, for instance), but If you just need basic rendering (which your screen shot implies) you can even inline the rendering function directly in the Select2 configuration object:
$('#my_select').select2({
// configuration options
renderTemplate: item => item.text || '\u200B',
// more configuration options
});