I am attempting to style the background color of some select2 options programmatically. For some reason it doesn't seem to work. I'm not seeing the background-color as red at all. Really what I am trying to do is set the background color for what select2 renders as an li and is the parent of the span tag in the formatText() function.
What you see here where those options are yellow, I want the whole row and not just the text to be yellow.
I also want to style individual rows in my Family drop-down to correspond with how they will be colored in my Products drop-down.
I have two sections of code that I think should be doing this. Here is the Fiddle - https://jsfiddle.net/jamiebrs/8axy7123/
const productsConfig = {
width: "100%",
theme: 'bootstrap4',
placeholder: "Select item",
allowClear: true,
templateSelection: formatText,
templateResult: formatText,
data: dataitems2,
escapeMarkup: function(m) {
return m;
}
};
function formatText (icon) {
if(icon.badge == undefined){
return $('<span>' + icon.text + '</span>');
} else {
return $('<span><span class="badge" style"background-color:red">'+ icon.badge.length + '</span> ' + icon.text + '</span>');
}
};
and....
$('#products').select2(
Object.assign({},
productsConfig,
{data: null}
)
);
$('#fam').on('change', function() {
// get selected value from the #fam select2
const selected = $(this).val();
// get the new products config (products config + data options)
const newProductsConfig = Object.assign({},
productsConfig,
{data: productOptions[selected]}
);
// destroy the existing products select2 and re-initialize
$('#products').empty().select2('destroy').select2(newProductsConfig);
$('#products').val(null).trigger('change')
});
